Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array includes vs object key lookup
(version: 0)
Comparing performance of:
includes vs lookup
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var b = { 1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}, 7: {}, 8: {}, 9: {}, 10: {} };
Tests:
includes
return a.includes(9)
lookup
return b[9]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
85495864.0 Ops/sec
lookup
154475792.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help explain the provided benchmark! **Benchmark Overview** The MeasureThat.net website is used to create and run JavaScript microbenchmarks, which allows users to compare the performance of different approaches on various devices. **Script Preparation Code** The script preparation code defines two variables: * `a`: an array with 10 elements (from 1 to 10) * `b`: an object with 10 properties, where each property has an empty object as its value. The keys of the object match the values in the `a` array. **Benchmark Definition** The benchmark definition consists of two test cases: 1. **includes**: This test case uses the `includes()` method on the `a` array to check if 9 is included. 2. **lookup**: This test case uses direct indexing on the object `b` to access its value at key 9. **Comparison Options** The benchmark compares two approaches: * Using the `includes()` method on an array * Directly accessing an object by key **Pros and Cons of each approach:** * **includes() method:** + Pros: - More readable and maintainable code (less magic numbers) - Easier to understand what's happening under the hood + Cons: - Potential performance overhead due to the method call - May not be optimized by the browser * **Direct indexing:** + Pros: - More direct and efficient access to object properties - Can potentially be optimized by the browser + Cons: - Less readable and maintainable code (more magic numbers) - May require more complex logic for handling errors or edge cases **Library Used:** None, as this is a basic benchmark without any external libraries. **Special JS Features/Syntax:** * **includes() method**: Introduced in ECMAScript 2015 (ES6), the `includes()` method provides a convenient way to check if an array includes a specific value. * **Direct indexing**: This syntax is a common pattern in JavaScript, but it's not specific to any particular feature or syntax. **Other Alternatives:** If you wanted to modify this benchmark to explore other approaches, here are some ideas: 1. Use `indexOf()` instead of `includes()`. 2. Add more elements to the array and object. 3. Use a different data structure, such as a Set or Map. 4. Implement a custom indexing mechanism for the object. 5. Compare the performance with alternative JavaScript implementations (e.g., Node.js, WebAssembly). I hope this explanation helps you understand the benchmark!
Related benchmarks:
set.has vs. array.includes
array includes vs object key lookup, large arrays
set.has vs. Object key lookup2
set.has vs. Object key in lookup
Comments
Confirm delete:
Do you really want to delete benchmark?