Searching in an array of objects
Date tested:
3 months ago
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Test name
Executions per second
For
1197297.0 Ops/sec
Array.find
18027210.0 Ops/sec
Array.findIndex
10713030.0 Ops/sec
Benchmark definition (click to collapse):
Script Preparation code:
const fruits = [ "Apple", "Banana", "Cherry", "Date", "Elderberry", "Fig", "Grape", "Honeydew", "Iced Raspberry", "Jackfruit", "Kiwifruit", "Lemon", "Mango", "Nectarine", "Orange", "Peach", "Pear", "Quince", "Raspberry", "Strawberry", "Tangerine", "Ugli Fruit", "Vine Fruit", "Watermelon", "Xigua" ]; var ARRAY = fruits.map((fruit) => ({name: fruit, desc: `Description ${fruit}`}));
Tests:
For
let el; for(var i=0; i < ARRAY.length; i++) { if(ARRAY[i].name == 'Jackfruit') { el = ARRAY[i]; break; } }
Array.find
const el = ARRAY.find((obj) => obj.name === 'Jackfruit');
Array.findIndex
const el = ARRAY[ARRAY.findIndex((obj) => obj.name === 'Jackfruit')];
Open this result on MeasureThat.net