Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.find vs object get
(version: 3)
Comparing performance of:
array.find 1st vs array.find middle vs array find last vs map.get vs iterate array vs iterate object vs iterate object predefined keys vs Object.entries
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]; var map = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10, a1: 11, b1: 12, c1: 13, d1: 14, e1: 15, f1: 16, g1: 17, h1: 18, i1: 19, j1: 20, a2: 21, b2: 22, c2: 23, d2: 24, e2: 25, f2: 26, g2: 27, h2: 28, i2: 29 } var keys = Object.keys(map)
Tests:
array.find 1st
arr.find(i => i === 1)
array.find middle
arr.find(i => i === 15)
array find last
arr.find(i => i === 29)
map.get
map.e
iterate array
let sum = 0 for (const i of arr) { sum += i }
iterate object
let sum = 0 for (const key of Object.keys(map)) { sum += map[key] }
iterate object predefined keys
let sum = 0 for (const key of keys) { sum += map[key] }
Object.entries
let sum = 0 for (const [key, value] of Object.entries(map)) { sum += value }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (8)
Previous results
Fork
Test case name
Result
array.find 1st
array.find middle
array find last
map.get
iterate array
iterate object
iterate object predefined keys
Object.entries
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):
Let's break down what is tested in the provided benchmark. **Benchmark Overview** The benchmark compares the performance of different approaches to access and manipulate arrays and objects in JavaScript. **Methods Compared** 1. `arr.find(i => i === 1)` (Array.find with callback) 2. `arr.find(i => i === 15)` (Array.find with callback, middle element) 3. `arr.find(i => i === 29)` (Array.find with callback, last element) 4. `map.e` (Object.get with bracket notation) 5. Iterating over arrays using `for` loops (`let sum = 0; for (const i of arr) { ... }`) 6. Iterating over objects using `for` loops (`let sum = 0; for (const key of Object.keys(map)) { ... }`) 7. Iterating over arrays and objects using `for...of` loops (`let sum = 0; for (const [key, value] of Object.entries(map)) { ... }`) **Pros and Cons** 1. **Array.find with callback**: Pros: * Efficient way to find the first or last element in an array. * No need to iterate over the entire array. Cons: * Requires a callback function, which can add overhead. 2. **Object.get (map.e)**: Pros: * Fast and efficient way to access a property by its key. * Uses the bracket notation, which is faster than dot notation or square brackets. Cons: * Only works with objects that have an `e` property (or any other property name). 3. **Iterating over arrays using for loops**: Pros: * Simple and easy to understand. Cons: * Can be slow, especially for large arrays. 4. **Iterating over objects using for loops**: Pros: * Similar to iterating over arrays. Cons: * May not be as efficient due to the overhead of object lookup. 5. **Iterating over arrays and objects using for...of loops**: Pros: * Fast and efficient way to iterate over both arrays and objects. Cons: * Not all browsers support `for...of` loops with objects. **Performance Results** The benchmark results show that: 1. Array.find with callback is the fastest method, especially for finding the first or last element. 2. Object.get (map.e) is also fast, but only works if the property exists. 3. Iterating over arrays using for loops is slower than array.find and object.get. 4. Iterating over objects using for loops is slower than iterating over arrays using for loops. **Conclusion** The benchmark highlights the importance of choosing the right method for your specific use case. If you need to find the first or last element in an array, use `arr.find` with a callback. For fast access to object properties, use `map.e`. When iterating over both arrays and objects, consider using `for...of` loops if available.
Related benchmarks:
Object.values Array.prototype.map vs Lodash.map
Object.entries Array.prototype.map vs Lodash.map
Object.values vs array index loop
Object key vs Array index access
Comments
Confirm delete:
Do you really want to delete benchmark?