Skip to content

Instantly share code, notes, and snippets.

@asduser
Last active July 2, 2021 10:33
Show Gist options
  • Save asduser/341c92554cc8dc2181a626711e3646a5 to your computer and use it in GitHub Desktop.
Save asduser/341c92554cc8dc2181a626711e3646a5 to your computer and use it in GitHub Desktop.
Performance difference between array.find vs array.indexOf. Result: indexOf is faster ;)
// 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);
}
}
// 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