Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.values() vs Object.keys().length small populated objects
(version: 0)
Need to compare populated objects between Object.values() and Object.keys() native method.
Comparing performance of:
Object.keys vs Object.values
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Script Preparation code:
window.obj = {}; for(i=0; i<100; i++){ window.obj[i] = i; }
Tests:
Object.keys
Object.keys(window.obj).length > 0;
Object.values
Object.values(window.obj).length > 0;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.keys
Object.values
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):
**Benchmark Overview** The provided benchmark compares the performance of two native JavaScript methods, `Object.values()` and `Object.keys().length`, when used to access properties of objects in small populated objects. **Options Compared** Two options are compared: 1. **`Object.values(window.obj).length > 0`**: This option uses the `Object.values()` method to extract an array of a given object's own enumerable property values and then checks if the length of this array is greater than 0. 2. **`Object.keys(window.obj).length > 0`**: This option uses the `Object.keys()` method to get an array of a given object's own enumerable property names and then checks if the length of this array is greater than 0. **Pros and Cons** * **`Object.values()`**: * Pros: More readable, as it explicitly expresses the intention of extracting values. However, it requires JavaScript versions 21.1.0 or higher. * Cons: May be slower due to the overhead of creating an array from a single object's properties. * **`Object.keys().length`**: * Pros: Faster in older JavaScript versions and can provide better performance for certain use cases. * Cons: Less readable, as it requires manual array creation and checks. **Library** The `lodash.min.js` library is used in the benchmark. It provides utility functions for working with arrays and objects. The specific function used here (`_.values`) converts an object to a values array without modifying the original object. **Special JS Feature/Syntax** There are no special JavaScript features or syntaxes mentioned in the provided code. **Other Alternatives** * **`Object.getOwnPropertyNames()`**: Similar to `Object.keys()`, but returns all properties, including non-enumerable ones. However, it is not as commonly used and might be slower due to its broader scope. * **Manual loop**: You could write a simple loop to iterate over the object's properties using `in` operator or `for...in` loop. This approach would require more manual effort but might provide fine-grained control. **Benchmark Preparation Code Explanation** The preparation code creates an object named `window.obj`, which is then populated with 100 properties using a `for` loop: ```javascript window.obj = {}; for (i = 0; i < 100; i++) { window.obj[i] = i; } ``` This ensures that the benchmark measures performance for small populated objects. **Individual Test Cases** Each test case defines a single benchmark that checks if either `Object.values()` or `Object.keys().length` returns a non-zero value. This is done to isolate the overhead of each method without relying on external state: ```javascript { "Benchmark Definition": "Object.keys(window.obj).length > 0;", "Test Name": "Object.keys" } { "Benchmark Definition": "Object.values(window.obj).length > 0;", "Test Name": "Object.values" } ``` These test cases focus on the performance difference between accessing object values and keys.
Related benchmarks:
lodash.keys vs Object.keys
lodash.keys vs Object.keys
_.isEmpty vs Object.keys.length vs Object.values.length
_.isEmpty() vs Object.keys().length populated objects
Comments
Confirm delete:
Do you really want to delete benchmark?