Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
values
(version: 0)
Comparing performance of:
For In vs Object keys forEach vs Object values forEach
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var obj = new Object() var keys = (new Array(100)).fill(0).map((x, i) => { return i + 1 }) keys.forEach((x) => { obj['prop' + x] = x })
Tests:
For In
for (var key in obj) { if (obj.hasOwnProperty(key)) console.log(obj[key]) }
Object keys forEach
Object.keys(obj).forEach(key => console.log(obj[key]))
Object values forEach
Object.values(obj).forEach(val => console.log(val))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
For In
Object keys forEach
Object values forEach
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):
**Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark with three test cases, each testing different approaches for iterating over an object's properties. **Tested Approaches** The benchmark compares the following approaches: 1. **For In Loop**: This approach uses the `for...in` loop to iterate over the object's properties. 2. **Object.keys() + forEach**: This approach uses the `Object.keys()` method to get an array of the object's property names, and then uses the `forEach()` method to iterate over the array. 3. **Object.values() + forEach**: This approach uses the `Object.values()` method to get an array of the object's values, and then uses the `forEach()` method to iterate over the array. **Pros and Cons** * **For In Loop**: + Pros: Simple and straightforward, no need to explicitly check for property existence. + Cons: Can be slower due to the loop's overhead, and may not work as expected in certain cases (e.g., when the object has inherited properties). * **Object.keys() + forEach**: + Pros: Efficient way to get an array of property names, and the `forEach()` method is optimized for arrays. + Cons: Requires creating an additional array of property names, which can be memory-intensive for large objects. * **Object.values() + forEach**: + Pros: Efficient way to get an array of values, and the `forEach()` method is optimized for arrays. + Cons: Requires creating an additional array of values, which can be memory-intensive for large objects. **Library/Function** In this benchmark, the following JavaScript functions are used: * `Object.keys()`: Returns an array of a given object's property names. * `Object.values()`: Returns an array of a given object's values. * `forEach()`: A method that executes a callback function for each element in an array. **Special JS Feature/Syntax** None of the benchmark cases use any special JavaScript features or syntax, such as async/await, promises, or modern language features like classes or destructuring. **Other Alternatives** If you wanted to write this benchmark yourself, here are some alternative approaches: * Use `for...of` loop instead of `for...in`, which can be more efficient for iterating over an object's own properties. * Use the `Object.entries()` method instead of `Object.keys()`, which returns an array of key-value pairs and can be more memory-efficient. * Consider using a library like Lodash or Ramda, which provide efficient and convenient ways to iterate over objects and arrays. **Benchmark Preparation Code** The script preparation code creates an object with 100 properties, each assigned a value from 1 to 100. This allows the benchmark to test the performance of different approaches on a large dataset. ```javascript var obj = new Object(); var keys = (new Array(100)).fill(0).map((x, i) => { return i + 1 }); keys.forEach((x) => { obj['prop' + x] = x }); ``` **Individual Test Cases** Each test case has a brief description and the benchmark definition in JSON format. The `Test Name` field indicates which approach is being tested. ```json [ { "Benchmark Definition": "for (var key in obj) {\r\n if (obj.hasOwnProperty(key)) console.log(obj[key])\r\n}", "Test Name": "For In" }, { "Benchmark Definition": "Object.keys(obj).forEach(key => console.log(obj[key]))", "Test Name": "Object keys forEach" }, { "Benchmark Definition": "Object.values(obj).forEach(val => console.log(val))", "Test Name": "Object values forEach" } ] ```
Related benchmarks:
Some benchmark
For in vs Object.*.forEach vs Object.values o++ - 1
For in vs Object.*.forEach vs Object.values vs _.forEach(_.values v3
For in vs Object.*.forEach vs Object.values vs _.forEach(_.values vs n=arr.length
Object.entries vs Object.values basic
Comments
Confirm delete:
Do you really want to delete benchmark?