Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
keys vs values
(version: 0)
Comparing performance of:
keys vs values
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var testObj = {} for (let i = 0; i < 10000; i++) { const randomString = Math.random().toString(36).substr(2, 5); testObj[randomString] = randomString; }
Tests:
keys
console.log(Object.keys(testObj));
values
console.log(Object.values(testObj));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
keys
values
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:148.0) Gecko/20100101 Firefox/148.0
Browser/OS:
Firefox 148 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
keys
4348.5 Ops/sec
values
1914.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested, compared, and considered. **Benchmark Definition** The benchmark is defined in two parts: 1. **Script Preparation Code**: This code creates an object `testObj` with 10,000 properties, each containing a random string as its value. 2. **Html Preparation Code**: There is no HTML preparation code provided for this benchmark, which means that the browser doesn't need to render any HTML content before running the test. **Test Cases** There are two individual test cases: 1. **"keys"`**: This test case measures how long it takes to get an array of keys (i.e., property names) from the `testObj` object using `Object.keys()`. 2. **"values"`**: This test case measures how long it takes to get an array of values from the `testObj` object using `Object.values()`. **Comparison** The benchmark compares two approaches: 1. **Using keys**: This approach uses `Object.keys()` to retrieve the array of keys. 2. **Using values**: This approach uses `Object.values()` to retrieve the array of values. **Options Compared** These options are compared in terms of execution time, which is measured in executions per second (i.e., how many times the test can be repeated in one second). **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Using keys (`Object.keys()`)** + Pros: More efficient when dealing with sparse objects or objects that contain fewer properties. + Cons: May be slower for dense objects (i.e., objects with many properties). * **Using values (`Object.values()`)** + Pros: Faster for dense objects, as it doesn't require iterating over the object's keys to retrieve its properties. + Cons: May be slower for sparse objects or objects that contain fewer properties. **Library and Special JS Features** In this benchmark: * No libraries are used explicitly. However, the `Object` and `Array` constructors are implicitly used in the test cases. * No special JavaScript features (e.g., async/await, promise) are used in the test code. **Other Alternatives** If you wanted to modify this benchmark, here are some alternative approaches you could consider: 1. Compare using other array methods, such as `Object.keys()` with a `for...of` loop or `Array.from(Object.keys(testObj))`. 2. Use a different data structure, like an array of objects, and compare the performance of getting keys or values from it. 3. Measure the performance of getting keys or values using different JavaScript engines or Node.js versions. Keep in mind that the performance differences between these approaches may vary depending on your specific use case and JavaScript environment.
Related benchmarks:
for in break vs object keys
for in break vs object keys smaller
for in break vs object keys better
for in break vs object keys really better
Comments
Confirm delete:
Do you really want to delete benchmark?