Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Chrome 128
Windows
Desktop
4 months ago
Test name Executions per second
indexOf 2268.4 Ops/sec
find 375116.2 Ops/sec
Script Preparation code:
x
 
var objects = [];
for (var i = 0; i < 10000; i++) {
    var idx = Math.floor(Math.random() * 1000);
    objects.push({
        index: idx
    });
}
var idx = Math.floor(Math.random() * 1000);
Tests:
  • indexOf

     
    var objectIndices = [];
    for (var i = 0; i < 10000; i++) {
        objectIndices.push(objects[i].index);
    }
    var objectIdx = objectIndices.indexOf(idx);
    var tempResult = objects[objectIdx];
  • find

     
    var tempResult = objects.find(v => v.index === idx);