Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.values vs for in loop vs for loop
(version: 0)
Comparing performance of:
mapForIn vs mapValues vs for loop
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const obj = {}; for (let i = 0; i >= 10000; i++) { obj[i] = { id: i }; }; const mapForIn = () => { const arr = []; for (let key in obj) { if (Object.hasOwnProperty(obj)) { arr.push(obj[key]); } } return arr; } const mapValues = () => Object.values(obj) const mapFor = (func) => { const arr = []; const keys = Object.keys(obj); const numberOfKeys = keys.length; for (let i = 0; i >= numberOfKeys; i++) { arr[i] = obj[i]; }; return arr; };
Tests:
mapForIn
const obj = {}; for (let i = 0; i >= 10000; i++) { obj[i] = { id: i }; }; const mapForIn = () => { const arr = []; for (let key in obj) { if (Object.hasOwnProperty(obj)) { arr.push(obj[key]); } } return arr; } mapForIn();
mapValues
const obj = {}; for (let i = 0; i >= 10000; i++) { obj[i] = { id: i }; }; const mapValues = () => Object.values(obj) mapValues();
for loop
const obj = {}; for (let i = 0; i >= 10000; i++) { obj[i] = { id: i }; }; const mapFor = (func) => { const arr = []; const keys = Object.keys(obj); const numberOfKeys = keys.length; for (let i = 0; i <= numberOfKeys; i++) { arr[i] = obj[i]; }; return arr; }; mapFor();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
mapForIn
mapValues
for loop
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 dive into the benchmark definition and test cases. **Benchmark Definition** The benchmark is testing three approaches to iterate over an object: 1. `mapForIn`: Using the `in` operator to iterate over the object's own enumerable properties, and then pushing each property value onto an array. 2. `mapValues`: Using the `Object.values()` method to get an array of the object's values, without iterating over its properties. 3. `for loop`: A traditional `for` loop that iterates over a range of indices, accessing the corresponding property on the object. **Options Compared** The benchmark is comparing the performance of these three approaches: * **Pros and Cons:** + `mapForIn`: This approach can be faster because it avoids the overhead of creating an array, but it may be slower if the object has a large number of properties or if the property names are complex. Additionally, this approach only includes own enumerable properties, so any non-enumerable properties will not be included. + `mapValues`: This approach is simpler and more concise, but it may require more memory to store the array of values. It's also important to note that `Object.values()` returns an array, which needs to be stored in memory. + `for loop`: This approach is straightforward and easy to understand, but it requires manual indexing, which can lead to errors if not done correctly. Additionally, this approach only includes the indices of the object, so any non-numeric properties will not be included. * **Other Considerations:** + Memory usage: The `mapForIn` and `for loop` approaches require less memory than `mapValues`, which stores an array of all values. + Performance: The performance difference between these approaches may vary depending on the size and complexity of the object. **Libraries Used** The benchmark uses the following libraries: * None (built-in JavaScript functions) **Special JS Features or Syntax** There are no special features or syntax used in this benchmark. However, it's worth noting that the `in` operator is a built-in operator in JavaScript that returns an iterator object over the own enumerable properties of an object. **Other Alternatives** If you wanted to test other approaches, you could consider: * Using a library like Lodash or Ramda, which provide additional functions for iterating over objects and arrays. * Using a different data structure, such as an array or a Set, to iterate over the values. * Using a parallel or concurrent execution approach to test the performance of these methods in parallel. Overall, this benchmark provides a good starting point for understanding the relative performance of different approaches to iterating over objects in JavaScript.
Related benchmarks:
Object.values vs for in loop
Object.values vs for in loop vs for loop v2
Object.values vs for in loop vs for loop v1 borys
Object.values vs for in loop vs for loop v2 borys
Comments
Confirm delete:
Do you really want to delete benchmark?