Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
object looping
(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) { if (a.hasOwnProperty(key)) { 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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
values
16446.6 Ops/sec
for in
7633.5 Ops/sec
keys for
9906.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! The provided JSON represents a benchmarking test case for measuring the performance of different approaches to iterate over an object in JavaScript. The benchmark is designed to test the efficiency of three methods: 1. `Object.values()` (test name: "values") 2. Iterating over object properties using the `for...in` loop (test name: "for in") 3. Using the `Object.keys()` method and iterating over the resulting array (test name: "keys for") **What is tested?** In this benchmark, we're testing how quickly each approach can iterate over an object, pushing its values into an array. **Approach comparison** Here's a brief overview of each approach: ### 1. `Object.values()` (`values` test) This method returns an array containing the object's property values. It's a concise and modern way to access an object's properties without having to use a loop or `for...in`. Pros: * Easy to read and write * Fast, as it avoids the overhead of iterating over properties Cons: * May not be suitable for older browsers or environments that don't support this method ### 2. Iterating over object properties using `for...in` (`for in` test) This approach uses a traditional loop to iterate over an object's property names, and then accesses the corresponding values using the `hasOwnProperty()` method. Pros: * Wide browser support * Easy to implement for those familiar with this syntax Cons: * May be slower than modern methods like `Object.values()` * More verbose compared to other approaches ### 3. Using `Object.keys()` and iterating over the resulting array (`keys for` test) This approach uses the `Object.keys()` method to get an array of property names, and then iterates over this array using a traditional loop. Pros: * Fast, as it avoids the overhead of accessing property values directly * Wide browser support Cons: * More verbose compared to `Object.values()` * May not be suitable for older browsers or environments that don't support this method **Library and special features** In this benchmark, there is no library used. The tests are written in vanilla JavaScript. As for special JavaScript features, the only notable feature is the use of template literals (`\t` instead of `\n`) in the test preparation code. This allows for more readable and concise code. **Other alternatives** If you were to write a benchmark like this today, you might also consider adding tests for other approaches, such as: * Using `for...of` loops * Utilizing modern Array methods like `forEach()` or `map()` * Implementing custom iteration logic using iterators or generators However, the original approach is still a valid and useful way to measure performance differences in JavaScript object iteration. I hope this explanation helps you understand the benchmarking test case!
Related benchmarks:
Object.keys vs Object.values
Javascript iterate object keys
Object.keys(obj)[0] vs for in
For in vs Object.*.forEach vs Object.keys
For in vs Object.*.forEach vs Object.values vs _.forEach(_.values v3
Comments
Confirm delete:
Do you really want to delete benchmark?