Skip to content

Instantly share code, notes, and snippets.

@cviebrock
Last active February 7, 2018 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cviebrock/f4bb24a3a8e3be5445a0c889b81dd199 to your computer and use it in GitHub Desktop.
Save cviebrock/f4bb24a3a8e3be5445a0c889b81dd199 to your computer and use it in GitHub Desktop.
Test JS loop speed
var N = 100000,
array = Array.apply(null, {length: N}).map(Function.call, Math.random),
max = 0,
start = new Date().getTime(),
len = array.length;
for(var i=0; i<len; i++) {
max = Math.max(max, array[i]);
}
var end = new Date().getTime();
console.log(max);
console.log('pre-calc length: '+(end-start)+' msec');
max = 0;
start = new Date().getTime();
for(var i=0; i<array.length; i++) {
max = Math.max(max, array[i]);
}
var end = new Date().getTime();
console.log(max);
console.log('no-calc length: '+(end-start)+' msec');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment