Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.values overhead cost 1
(version: 0)
Comparing performance of:
Call Object.values on each execution vs Use shared Object.values
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var myObj = {}; for (var i = 0; i < 1000; i++) { myObj['val' + i] = 'val' + i; } var myObjValues = Object.values(myObj);
Tests:
Call Object.values on each execution
var curObjValues = Object.values(myObj);
Use shared Object.values
var curObjValues = myObjValues;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Call Object.values on each execution
Use shared Object.values
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Call Object.values on each execution
15579.3 Ops/sec
Use shared Object.values
32293358.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Definition** The provided JSON represents a benchmark definition, which is a set of instructions for creating and running a specific test case. In this case, we have two test cases: 1. "Object.values overhead cost 1" 2. "Call Object.values on each execution" The first test case creates an object `myObj` with 1000 properties, populates it with string values, and then measures the overhead of calling `Object.values()` on this object. 2. The second test case creates a shared reference to `myObjValues`, which is the result of calling `Object.values(myObj)`, and then measures the performance of calling `Object.values` on this shared reference. **Options compared** In both test cases, we have two options: 1. **Call Object.values() on each execution**: This approach creates a new local variable `curObjValues` to store the result of `Object.values()` for each individual call. 2. **Use shared Object.values**: This approach creates a single reference `myObjValues` and shares it between calls. **Pros and Cons** **Call Object.values() on each execution** Pros: * Easier to understand and debug, as each call is independent and has its own scope. * May provide more accurate results for certain use cases (e.g., when the object being modified is not the same as the one being iterated). Cons: * Can lead to performance overhead due to repeated function calls and variable assignments. **Use shared Object.values** Pros: * Reduces overhead due to repeated function calls and variable assignments. * Can provide better performance for large datasets, as the reference is only created once. Cons: * Can be more challenging to understand and debug, as the shared reference must be carefully managed to avoid side effects. * May not be suitable for use cases where the object being iterated is different from the one being modified. **Library: Object.values()** `Object.values()` is a method introduced in ECMAScript 2020 (ES10) that returns an array of values owned by an object. It's implemented as a specialized function call, which means it has lower overhead compared to iterating over an object using `for...in` or `forEach`. **Special JS feature: Arrow functions** In the provided benchmark code, we see arrow functions used in both test cases: ```javascript var curObjValues = Object.values(myObj); ``` Arrow functions were introduced in ECMAScript 2015 (ES6) and provide a concise way to define small functions without declaring `this` explicitly. They are generally considered more readable and efficient than traditional function expressions. **Other alternatives** In addition to the two options compared, there may be other approaches that could be used to measure the overhead of calling `Object.values()`: * **Using a native array**: Instead of using `Object.values()`, you could create an array with the same values using the spread operator (`[...myObj]`) or `Array.from()`. This approach would eliminate the overhead of the specialized function call. * **Using a custom iterator**: You could implement a custom iterator for the object, which would allow you to iterate over its values without calling `Object.values()` explicitly.
Related benchmarks:
Reading object values in loop: Object.keys vs Object.values
Object.values overhead cost
Object.values overhead cost 4
Object.values overhead cost1
Comments
Confirm delete:
Do you really want to delete benchmark?