Last active
February 7, 2018 21:41
-
-
Save cviebrock/f4bb24a3a8e3be5445a0c889b81dd199 to your computer and use it in GitHub Desktop.
Test JS loop speed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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