Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6969589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:36:01+00:00 2026-05-27T16:36:01+00:00

var arr=[]; var k; for(var i=0;i<5000;i++) arr[i]=i; console.time(native loop); var len=arr.length; for(var j=0;j<len;j++) k=arr[j];

  • 0
var arr=[];
var k;
for(var i=0;i<5000;i++)
 arr[i]=i;

console.time("native loop");
var len=arr.length;
for(var j=0;j<len;j++)
 k=arr[j];
console.timeEnd("native loop");

console.time("jq loop");
$(arr).each(function(i,el){
 k=el;
});
console.timeEnd("jq loop");

It is generating, 14ms for native loop and 3ms for Jquery each.

I’m using Jquery 1.6.2. If Jquery uses native loop behind the scene, then how come, this is possible??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:36:02+00:00Added an answer on May 27, 2026 at 4:36 pm

    I would think that it has to do with the scope of your index variable — your j variable is a global, which is about the slowest case of variable access. Every time that your loop has to reference j, it needs to check all the way up the scope chain, back to the global object, and then get the variable value from there.

    I get similar numbers to you in my console (Chrome, OS X — 13-15ms for the for loop, 3-4ms for jQuery).

    But, if I do this:

    (function() {
        console.time("native loop with function scope");
        var len=arr.length;
        for(var j=0;j++<len;)
            k=arr[j];
        console.timeEnd("native loop with function scope");})()
    

    It executes in just 5ms.

    The difference in this case is that j is a function-local variable, available immediately in the first place the JavaScript engine looks for variables. len is similarly local; the only globals in this case are k and arr.

    To get even more speed out of this, make k a function-scope variable, and pass in arr as a parameter:

    (function(arr) {
        console.time("native loop 2");
        var len=arr.length, k;
        for(var j=0;j++<len;)
            k=arr[j];
        console.timeEnd("native loop 2");})(arr)
    
    > native loop 2: 0ms
    

    Well, that’s a bit too fast now. Maybe arr should be bigger:

    var arr=[];
    for(var i=0;i<50000;i++)
        arr[i]=i;
    
    [Try again...]
    > native loop 2: 1ms
    

    That slowed it down a bit.

    With a 500k-element array, this code runs in 4ms in the JavaScript console on my machine. At 5M, it takes 36ms.

    You should also note that you aren’t using a raw jQuery.each() call — you are first wrapping your array in $(), creating a jQuery object, and then calling .each on that. A fairer test might be to run

    $.each(arr, function(i,el){
        k=el;
    });
    

    That should be pretty close to the timing of the second example above. jQuery adds a bit of overhead; checking the types of its arguments, but after that it runs a pretty tight loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

var arr = []; $('#menu').children().each(function(){ arr = $(this).width(); }); console.log(arr); It doesn't work that
JavaScript/JQuery var arr=[]; $('.element').each(function(i) { arr.push({id:i,value:$(this).attr('data-value')}); }); $.get('/json.php?arr='+arr,function(result) { alert(result); }); PHP <?php $j
var arr = [[1,2,3],[4,5,6],[7,8,9]]; function out(ar){ var interval = setInterval(function(){ for (var i=0; i<ar.length;i++){
function calcTotalScore(){ var arr = [A,B,C,D,E,F]; $.each(arr, function(n,val){ calcTotal(val); }); } calcTotal(value){ // sample
function getSelectedCopyDates() { var arr = new Array(); //for every row that has a
I have the following : <script> $().ready(function() { $(#myVal#).blur(function() { var arr = jQuery.makeArray(
var arr = ['test','hello']; is there a javascript native call to get index of
function whatTheHeck(obj){ var arr = [] for(o in obj){ arr.concat([what]) } return arr }
$(document).ready(function() { $('a.menuitem').click(function() { var arr = 0; var link = $( this ),
Let us consider the following JavaScript snippet var arr = []; function pushMe() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.