Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Summing an object
(version: 4)
Comparing performance of:
For in vs Obj.keys for loop vs Object.keys reduce vs For in function vs Object.keys for loop function vs Object.keys reduce function vs Object.keys for loop 2
Created:
9 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var obj = { "a": 1, "b": 2, "c": 3, "d": 4 };
Tests:
For in
var sum = 0; for (var val in obj) { sum += obj[val]; }
Obj.keys for loop
var sum = 0; var keys = Object.keys(obj); for (var i=0; i< keys.length; i++) { sum += obj[keys[i]]; }
Object.keys reduce
function add(sum, key) { return sum + obj[key]; } Object.keys(obj).reduce(add, 0);
For in function
function sumObj(obj) { var sum = 0; for (var val in obj) { sum += obj[val]; } return sum; } sumObj(obj);
Object.keys for loop function
function sumObj(obj) { var sum = 0; var keys = Object.keys(obj); for (var i=0; i< keys.length; i++) { sum += obj[keys[i]]; } return sum; } sumObj(obj);
Object.keys reduce function
function sumObj(obj) { function add(sum, key) { return sum + obj[key]; } return Object.keys(obj).reduce(add, 0); } sumObj(obj);
Object.keys for loop 2
function sumObj(obj) { var sum = 0; for (var i=0; i< Object.keys(obj).length; i++) { sum += obj[Object.keys(obj)[i]]; } return sum; } sumObj(obj);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (7)
Previous results
Fork
Test case name
Result
For in
Obj.keys for loop
Object.keys reduce
For in function
Object.keys for loop function
Object.keys reduce function
Object.keys for loop 2
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):
I'd be happy to explain what's being tested in the provided benchmark. The goal of this benchmark is to measure how fast different approaches can sum up the values of an object. The object contains four key-value pairs: "a" with value 1, "b" with value 2, "c" with value 3, and "d" with value 4. Here are the four test cases: 1. **For-in**: This approach uses the `for...in` loop to iterate over the object's keys and add up their corresponding values. 2. **Object.keys for loop**: This approach uses the `Object.keys()` method to get an array of the object's keys, then iterates over that array using a traditional `for` loop to add up the values. 3. **Object.keys reduce**: This approach uses the `reduce()` method with an anonymous function to sum up the values. The anonymous function takes two arguments: the accumulator (initially set to 0) and the current key. It returns the accumulator plus the value associated with that key. 4. **For-in function**: This approach defines a custom function called `sumObj` that uses the `for...in` loop to sum up the object's values, similar to test case 1. 5. **Object.keys for loop function**: Similar to test case 3, this approach defines a custom function called `sumObj` that uses `Object.keys()` and a traditional `for` loop to sum up the object's values. 6. **Object.keys reduce function**: This approach is similar to test case 4, but uses an anonymous function with `reduce()` to sum up the values. The benchmark measures the time it takes to execute each test case on various browsers (in this case, Chrome 53). **Pros and Cons of different approaches:** * **For-in**: Pros: simple and straightforward. Cons: can be slower due to the way JavaScript handles object iteration. * **Object.keys for loop**: Pros: efficient use of modern JavaScript features. Cons: may require additional imports or setup if not using modern browsers. * **Object.keys reduce**: Pros: concise and efficient, leveraging `reduce()` for summing values. Cons: can be less intuitive for some developers, especially those without experience with functional programming. **Other considerations:** * **Performance overhead**: The benchmark measures the raw execution time of each test case. However, other factors like memory allocation, garbage collection, or disk I/O might affect overall performance. * **Browser compatibility**: The benchmark is run on a specific version of Chrome. Results may vary across different browsers, versions, and platforms. The benchmark's primary goal is to provide a neutral comparison of the efficiency of these four approaches for summing up an object's values. By analyzing the execution times, developers can choose the most efficient approach for their use cases.
Related benchmarks:
For in vs Object.keys.forEach FixedForYaRetard
Object.values, For...in, Object.keys Object.entires
Object Values vs for .. in
ok5}O.54<'fn9ZN2KFz=
Comments
Confirm delete:
Do you really want to delete benchmark?