/*
ARRAYS & ITERATIONS
http://jsperf.com/native-loop-performance/14
*/
//////////////////////////////////////////////////////////////////
// Preparation code + jQuery underscore in the body
// Re-Populate the Array for each test because some methods are "destructives" ("Array.pop()" ...)
var arr = [];
//////////////////////////////////////////////////////////////////
// Testing rendered values for each methods
// http://jsperf.com/native-loop-performance/14 : Test //-> Ops/sec
var log = (function() {
var logger = document.body;
return function(mess) { logger.innerHTML += typeof(mess)+' : '+mess+'<br />'; };
}());
var arr = [];
for (var i = 0; i < 3; i++) arr[i] = i;
// THE TESTS !
log('----- Test 1 -----');
for (var i = 0; i < arr.length; i++) log(arr[i])
log('----- Test 2 -----');
for (var i = 0, length = arr.length; i < length; i++) log(arr[i])
log('----- Test 3 -----');
for (var i = arr.length - 1; i >= 0; i--) log(arr[i])
log('----- Test 4 -----');
for (var key in arr) log(arr[key])
log('----- Test 5 -----');
var i = 0;
while (i++ < arr.length) log(arr[i - 1])
log('----- Test 6 -----');
var length = arr.length;
while (length--) log(arr[length])
log('----- Test 7 -----');
var arr2 = arr.slice();
while(arr2.length) log(arr2.pop())
log('----- Test 8 -----');
var arr2 = arr.slice();
while(arr2.length) log(arr2.shift())
log('----- Test 9 -----');
var arr2 = arr.slice(); // Copy
do { log(arr2.shift())} while(arr2.length)
log('----- Test 10 -----');
arr.forEach(function(value) { log(value) })
log('----- Test 11 -----');
arr.map(function(value) { log(value) })
log('----- Test 12 -----');
$.each(arr, function(value) { log(value) })
log('----- Test 13 -----');
_.each(arr, function(value) { log(value) })
log('----- Test 14 -----');
try { // Only work in #FF ?
eval("[log(arr[key]) for (key in arr )]");
}
catch(e) {}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.