Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.values vs for in loop vs for loop v2 borys
(version: 0)
Comparing performance of:
mapValues (every item uniq) vs for loop (every item uniq) vs mapValues (all items same) vs for loop (all items same)
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Tests:
mapValues (every item uniq)
const obj = []; for (let i = 0; i <= 10000; i++) { obj.push({ id: i }); }; const mapValues = () => { const inputs = Object.values(obj).map(r => r.id); const hasMulti = _.uniq(inputs).length > 1; return hasMulti; } mapValues();
for loop (every item uniq)
const obj = []; for (let i = 0; i <= 10000; i++) { obj.push({ id: i }); }; const mapFor = () => { var m = new Map(); var values = Object.values(obj); for (let i = 0; i < values.length; i++) { var id = values[i].id; m.set(id, 0); if (m.size > 1) { return true; } }; return false; }; mapFor();
mapValues (all items same)
const obj = []; for (let i = 0; i <= 10000; i++) { obj.push({ id: 1 }); }; const mapValues = () => { const inputs = Object.values(obj).map(r => r.id); const hasMulti = _.uniq(inputs).length > 1; return hasMulti; } mapValues();
for loop (all items same)
const obj = []; for (let i = 0; i <= 10000; i++) { obj.push({ id: 1 }); }; const mapFor = () => { var m = new Map(); var values = Object.values(obj); for (let i = 0; i < values.length; i++) { var id = values[i].id; m.set(id, 0); if (m.size > 1) { return true; } }; return false; }; mapFor();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
mapValues (every item uniq)
for loop (every item uniq)
mapValues (all items same)
for loop (all items same)
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 dive into explaining the benchmark. **Overview** The provided JSON represents a JavaScript microbenchmarking test case, which compares the performance of three different approaches: using `Object.values()` with the `map` function (`mapValues`), a traditional `for` loop with an array (`for loop (every item uniq)`), and another traditional `for` loop with an array, but with all items having the same value (`for loop (all items same)`). The test also uses the Lodash library. **Options Compared** The benchmark compares the following options: 1. **`mapValues`**: Uses `Object.values()` to get an array of values from an object and then maps over it using the `map` function to count the number of unique elements. 2. **Traditional `for` loop (every item uniq)**: Iterates over the array created by `Object.values()` using a traditional `for` loop, checks if each element is unique, and returns true if more than one element is unique. 3. **Traditional `for` loop (all items same)**: Similar to the previous option, but with all elements having the same value. **Pros and Cons** * **`mapValues`**: Pros: * Simple and concise code. * Uses modern JavaScript features. Cons: * May have performance overhead due to the use of `map()` and `uniq()`. * Traditional `for` loop (every item uniq): Pros: * Avoids potential overhead of modern JavaScript features. Cons: * More verbose code. * Less readable for complex logic. * Traditional `for` loop (all items same): Similar pros and cons as the previous option. **Lodash Library** The Lodash library is used to implement the `uniq()` function, which returns an array with unique elements. The `mapValues` test case uses this function to count the number of unique elements in the array created by `Object.values()`. While using a third-party library can provide convenience and efficiency, it also introduces dependencies that might affect performance. **Other Considerations** * **Data Structure**: The benchmark uses an object with 10,000 properties to create an array of values. This data structure may not be representative of real-world scenarios, where objects are often used for more complex data structures. * **Performance**: The benchmark measures the execution time per second for each test case on a desktop device running Chrome 104. The results indicate that `mapValues` outperforms the traditional `for` loops in this specific scenario. **Alternatives** If you were to reimplement these benchmarks without using Lodash, you could: 1. Use the native JavaScript methods `Set` or `Map` instead of `uniq()` to count unique elements. 2. Implement the `mapValues` logic from scratch using simple array operations. 3. Consider using a different data structure, such as an array with unique values, to eliminate the need for the traditional `for` loops. Ultimately, the choice of approach depends on your specific requirements and performance constraints.
Related benchmarks:
lodash.each vs Object.forEach
Lodash _.forEach vs Object forEach
Lodash values vs Object.values
big lodash vs nativejs foreach
Lodash IsEmpty for objects
Comments
Confirm delete:
Do you really want to delete benchmark?