Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object copy loop compare
(version: 3)
Comparing performance of:
for-in vs Object.keys.forEach vs Object.entries.forEach vs Object.keys for
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const hasOwnProperty = Object.prototype.hasOwnProperty function has (obj, key) { return hasOwnProperty.call(obj, key) } const count = 10000 function loop (fn) { for (let i = count; i > 0; i--) fn() } var obj = { a: 1, b: 1, c: 1, d: 1, e: 1, f: 1, g: 1 } var result = {}
Tests:
for-in
loop(function () { for (const key in obj) { if (has(obj, key)) result[key] = obj[key] } })
Object.keys.forEach
loop(function () { Object.keys(obj).forEach(function (key) { result[key] = obj[key] }) })
Object.entries.forEach
loop(function () { Object.entries(obj).forEach(function ([key, value]) { result[key] = value }) })
Object.keys for
loop(function () { const keys = Object.keys(obj) const length = keys.length for (let i = length; i > 0; i--) { const key = keys[i] result[key] = obj[key] } })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for-in
Object.keys.forEach
Object.entries.forEach
Object.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):
**What is being tested?** The provided benchmark measures the performance of different ways to copy properties from an object (`obj`) to another object (`result`). The test focuses on four specific approaches: 1. `for-in` 2. `Object.keys.forEach` 3. `Object.entries.forEach` 4. `Object.keys for` Each approach uses a loop to iterate over the object's properties and assign their values to the result object. **Options being compared** The benchmark compares the performance of these four approaches: * `for-in`: A legacy way to iterate over an object's properties using the `in` operator. * `Object.keys.forEach`: Uses the `Object.keys()` method to get an array of property names and then iterates over it using the `forEach()` method. * `Object.entries.forEach`: Uses the `Object.entries()` method to get an array of key-value pairs and then iterates over it using the `forEach()` method. * `Object.keys for`: A newer way to iterate over an object's properties using a simple `for` loop with the `key` variable. **Pros and Cons** Here are some pros and cons for each approach: 1. **for-in**: * Pros: Simple and widely supported, can be used in older browsers. * Cons: Can be slower due to the use of the `in` operator, which may not be optimized by modern engines. 2. **Object.keys.forEach**: * Pros: Modern and efficient, uses a built-in method to get property names. * Cons: May not work in older browsers that don't support `Object.keys()`. 3. **Object.entries forEach**: * Pros: Efficient and modern, uses a built-in method to get key-value pairs. * Cons: May require newer JavaScript engines or polyfills for older browsers. 4. **Object.keys for**: * Pros: Newer and efficient, uses a simple `for` loop with a variable. * Cons: Not as widely supported as the other approaches (only introduced in ECMAScript 2019). **Libraries used** The benchmark defines two custom functions: 1. `hasOwnProperty`: A function that checks if an object has a property with the given name, using the `Object.prototype.hasOwnProperty()` method. 2. `loop`: A function that takes another function as an argument and executes it repeatedly. **Special JS features or syntax** None of the individual test cases use any special JavaScript features or syntax beyond the standard ECMAScript 2015 features.
Related benchmarks:
For in vs Object.keys.forEach vs. Object.keys+for
For in vs For vs Object.keys.forEach
For in vs Object.keys for loop vs Object.keys.forEach
for-in with hasOwnProperty vs for-of over Object.keys
for-in hasOwnProperty vs object.keys for-of test vs for-in
Comments
Confirm delete:
Do you really want to delete benchmark?