Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.values vs for in loop vs for loop v1 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; if (m.has(id)) { return true; } m.set(id, 0); }; 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; if (m.has(id)) { return true; } m.set(id, 0); }; 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 break down the benchmark and its test cases for you. **Benchmark Overview** The benchmark measures the performance of three different approaches to check if an array has multiple unique values: 1. `Object.values` with `lodash uniq` 2. `for` loop 3. `Map` data structure with a simple iteration **Test Cases** There are four test cases, each measuring a specific scenario: ### Test Case 1: `mapValues (every item uniq)` * **Benchmark Definition**: Creates an array of 10,000 objects with unique `id` properties using `Object.values`. Then, uses the `lodash uniq` function to check if there are multiple unique values in the array. * **Test Name**: "mapValues (every item uniq)" * **Library Used**: `_` from Lodash, a utility library for JavaScript. **Pros and Cons:** * Pros: + Easy to implement and understand + Relies on the efficiency of `lodash uniq` * Cons: + May not be optimized for performance ### Test Case 2: `for loop (every item uniq)` * **Benchmark Definition**: Creates an array of 10,000 objects with unique `id` properties using a traditional `for` loop. Then, iterates through the array and checks if there are multiple unique values. * **Test Name**: "for loop (every item uniq)" * No library used. **Pros and Cons:** * Pros: + Easy to understand + No external dependencies * Cons: + May be slower due to unnecessary iterations ### Test Case 3: `mapValues (all items same)` * **Benchmark Definition**: Creates an array of 10,000 objects with identical `id` properties using `Object.values`. Then, uses the `lodash uniq` function to check if there are multiple unique values in the array. * **Test Name**: "mapValues (all items same)" * **Library Used**: `_` from Lodash. **Pros and Cons:** * Pros: + Similar to Test Case 1, but with a simpler use case * Cons: + Still relies on `lodash uniq` ### Test Case 4: `for loop (all items same)` * **Benchmark Definition**: Creates an array of 10,000 objects with identical `id` properties using a traditional `for` loop. Then, iterates through the array and checks if there are multiple unique values. * **Test Name**: "for loop (all items same)" * No library used. **Pros and Cons:** * Pros: + Similar to Test Case 2 * Cons: + May be slower due to unnecessary iterations **Alternative Approaches** Other approaches could include: 1. Using `Set` data structure to check for unique values. 2. Using a custom implementation of the `uniq` function, optimized for performance. 3. Comparing the performance of different JavaScript engines (e.g., V8, SpiderMonkey) using the same benchmark. These alternatives would require modifications to the benchmark definition and test cases to accommodate the new approaches. Keep in mind that this is just an overview, and a deeper analysis of each test case would require more detailed breakdowns.
Related benchmarks:
lodash.each vs Object.forEach
Lodash _.forEach vs Object forEach
Lodash values vs Object.values
Lodash IsEmpty for objects
lodash.each vs lodash.forEach vs Object.forEach
Comments
Confirm delete:
Do you really want to delete benchmark?