Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array of object search
(version: 5)
Comparing performance of:
Array.find vs indexed copy vs index map
Created:
9 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function randstr(N) { return (Math.random().toString(36)+'00000000000000000').slice(2, N+2); } var dataset = []; for (var x = 0; x < 100000; x++) { dataset.push({id: 'xx' + x, name: randstr(10), other: randstr(10)}); } var indexedDataset = {}; for (var item of dataset) { indexedDataset[item.id] = item; } var indexMap = {}; for (var [i, item] of dataset.entries()) { indexMap[item.id] = i; } function assertEq(a, b) { if (a !== b) { new Error(`Failed assert: "${a}" expected "${b}"`); } }
Tests:
Array.find
for (var x = 0; x < 10000; ++x) { const item = dataset.find(v => v.id == 'xx' + 123); assertEq(item.id, 'xx' + 123); }
indexed copy
for (var x = 0; x < 10000; ++x) { const item = indexedDataset['xx' + 123]; assertEq(item.id, 'xx' + 123); }
index map
for (var x = 0; x < 10000; ++x) { const item = dataset[indexMap['xx' + 123]]; assertEq(item.id, 'xx' + 123); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.find
indexed copy
index map
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared options, pros and cons, and other considerations. **Benchmark Overview** The benchmark measures the performance of three different approaches to search for an item in an array of objects: `Array.find()`, indexed copy, and index map. The test case creates a large dataset with 100,000 objects, each with a unique ID, and then searches for an object with a specific ID using each approach. **Approaches Compared** 1. **Array.find()**: This method uses the Array.prototype.find() function to search for an item in the array that satisfies the provided callback function. In this case, the callback function checks if the `id` property of the current item matches the target ID. 2. **Indexed Copy**: This approach creates a shallow copy of the original dataset using the spread operator (`...`) and then searches for the object with the target ID in the copied array. 3. **Index Map**: This approach creates an index map by iterating over the dataset and storing the item's ID as the key, along with its index. Then, it uses this index map to search for the object with the target ID. **Pros and Cons** * **Array.find()**: + Pros: concise and readable code, eliminates the need to manage indices. + Cons: can be slow due to the overhead of creating a callback function, and may not perform well on large datasets. * **Indexed Copy**: + Pros: simple and easy to understand, can be faster than Array.find() since it only needs to search through a smaller array. + Cons: creates an additional copy of the dataset, which can consume more memory, and the search operation is still O(n) in the worst case. * **Index Map**: + Pros: can be faster than Indexed Copy since it uses a hash-based lookup, reducing the number of iterations needed to find the target ID. + Cons: requires more code and understanding of index maps, and may not perform well if the dataset is very large or has many duplicate IDs. **Other Considerations** * **Memory Usage**: The benchmark measures the performance of each approach under different memory constraints. This helps determine which method is most efficient in terms of memory usage. * **CPU Usage**: The benchmark also measures CPU usage, as this can be an important factor in determining the overall performance of a system. **Library/Function Used** In this benchmark, two functions are used: 1. `Array.prototype.find()`: This function is part of the ECMAScript standard and provides a way to search for an item in an array. 2. Spread operator (`...`): This operator creates a shallow copy of an object or array. **Special JavaScript Features** In this benchmark, two special JavaScript features are used: 1. **Arrow functions**: The `assertEq()` function is defined using an arrow function, which allows it to be concise and readable while also providing closure over the variable `a` and `b`. 2. **Template literals**: The `randstr(N)` function uses template literals to generate a random string of length `N`. Overall, this benchmark provides valuable insights into the performance characteristics of different approaches to searching for an item in an array of objects, and can help developers choose the most efficient method for their specific use case.
Related benchmarks:
Array vs object literal
Array vs object literal v2
Search: Map.get vs Array.find
Search: Array to Map and find vs Array.find685000
Search: Array to Map and find vs Array.find 2
Comments
Confirm delete:
Do you really want to delete benchmark?