Skip to content

Instantly share code, notes, and snippets.

@asm-jaime
Created April 15, 2016 07:19
Show Gist options
  • Save asm-jaime/60a6c247fb3bb61ffc932e731cc0157a to your computer and use it in GitHub Desktop.
Save asm-jaime/60a6c247fb3bb61ffc932e731cc0157a to your computer and use it in GitHub Desktop.
measure performance indexOf with and without 'fromIndex'
'use strict'
let people = [];
let index = 0
let temp = 0
let ids = [];
for(let i = 0; i<10000; i++){
people.push({id:i, name : 'rnd'});
}
for(let i = 0; i<5000; i++){
ids.push(i+1);
}
console.time("quick")
people.forEach(function(item) {
if ((temp = ids.indexOf(item.id, index)) !== -1) {
index = temp;
//console.log(item.name);
}
});
console.timeEnd("quick")
console.time("slow")
people.forEach(function(item) {
if (ids.indexOf(item.id) !== -1) {
//console.log(item.name);
}
});
console.timeEnd("slow")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment