Last active
July 2, 2021 10:33
-
-
Save asduser/341c92554cc8dc2181a626711e3646a5 to your computer and use it in GitHub Desktop.
Performance difference between array.find vs array.indexOf. Result: indexOf is faster ;)
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
// ES5 | |
var list = []; | |
for (var i =0, len = 500000; i < len; i++) { | |
list.push(i); | |
} | |
console.time("test"); | |
repeater(20, {callback: test1, value: 499999, list: list} ); | |
console.timeEnd("test"); | |
function test1(val, list){ | |
return list.find(function(el){ return el == val }); | |
} | |
function test2(val, list){ | |
return list.indexOf(val); | |
} | |
function repeater(times, config){ | |
for (var i = 0; i < times; i ++) { | |
config.callback(config.value, config.list); | |
} | |
} |
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
// ES6 | |
let list = []; | |
for (var i =0, len = 500000; i < len; i++) { | |
list.push(i); | |
} | |
console.time("test"); | |
repeater(20, {callback: test1, value: 499999, list: list} ); | |
console.timeEnd("test"); | |
let test1 = (val, list) => { | |
list.find((el) => el == val); | |
}; | |
let test2 = (val, list) => { | |
list.indexOf(val); | |
}; | |
let repeater = (times, config) => { | |
for (var i = 0; i < times; i ++) { | |
config.callback(config.value, config.list); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment