Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
last values test
(version: 0)
Comparing performance of:
values vs for in vs keys for
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = {}; for (let i = 0; i < 1000; i++) { var key = 'adjaksdnmas'+i; a[key] = { id: key, prop1: true, pro2: 'asdäöasdlas', prop3: [1, 23, 42342, 523423] }; }
Tests:
values
var t = Object.values(a)
for in
var t = []; for (var key in a) { t.push(a[key]); }
keys for
var t = []; var keys = Object.keys(a) var length = keys.length; for (var j; j < length; j++) { t.push(a[keys[j]]); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
values
for in
keys for
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 the benchmark and its options. **Benchmark Definition:** The benchmark is designed to measure the performance of three different ways to iterate over an object's values: `Object.values()`, `for...in` loop, and using `Object.keys()` to get the keys and then iterating over them. **Options Compared:** 1. **`Object.values()`**: This method returns an array of a given object's own enumerable property values. 2. **`for...in` loop**: This is a traditional way to iterate over an object's properties, but it also includes non-enumerable properties and can be slower due to the overhead of iterating over all properties, even if they're not being used. 3. **Using `Object.keys()` to get keys and then iterating**: This approach involves getting the keys of the object using `Object.keys()`, and then iterating over them to access their corresponding values. **Pros and Cons:** * **`Object.values()`**: + Pros: Fast, efficient, and modern way to get an array of values. + Cons: Requires ECMAScript 2019 (ES10) or later for compatibility with older browsers. * **`for...in` loop**: + Pros: Wide browser support, as it's been around since ES5. + Cons: Can be slower due to the overhead of iterating over all properties, even if they're not being used. Also includes non-enumerable properties. * **Using `Object.keys()` and then iterating**: + Pros: Still fast and widely supported, but may have slightly higher overhead than `Object.values()` due to the additional step of getting keys. + Cons: May require more memory since we're storing the keys in an array. **Library Used:** None. The benchmark is self-contained and doesn't rely on any external libraries for its functionality. **Special JS Features/Syntax:** * None mentioned. All three approaches use standard JavaScript syntax. **Other Considerations:** When choosing between these approaches, consider the following: * If you need to support older browsers (e.g., IE 11), the `for...in` loop might be a safer choice. * If you're targeting modern browsers and want a fast, efficient way to iterate over an object's values, `Object.values()` is likely your best bet. **Alternative Approaches:** Some other ways to iterate over an object's values could include: * Using `for...of` loop (introduced in ES6) with the `[Symbol.iterator]()` method. * Utilizing a library like Lodash or Ramda, which provide utility functions for working with objects and arrays. These alternative approaches might offer different trade-offs in terms of performance, memory usage, and compatibility across browsers.
Related benchmarks:
Some benchmark
values, for in, keys for 10 items
values, for in, keys for part 2
For in vs Object.*.forEach vs Object.values vs _.forEach(_.values v3
Comments
Confirm delete:
Do you really want to delete benchmark?