Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
object.keys vs entries + filtering
(version: 0)
Comparing performance of:
Object.entries + filtering vs Object.keys + filtering
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var participants = { "14395101": { "hidden": false, "notificationPreference": "always" }, "24580932": { "hidden": true, "notificationPreference": "always" }, "31245789": { "hidden": false, "notificationPreference": "always" }, "49801234": { "hidden": true, "notificationPreference": "always" }, "56710248": { "hidden": false, "notificationPreference": "always" }, "62378901": { "hidden": true, "notificationPreference": "always" }, "78549210": { "hidden": false, "notificationPreference": "always" }, "84792154": { "hidden": true, "notificationPreference": "always" }, "91234560": { "hidden": false, "notificationPreference": "always" }, "10345678": { "hidden": true, "notificationPreference": "always" } };
Tests:
Object.entries + filtering
let participantsEntries = Object.entries(participants); const currentUserAccountID = 13; participantsEntries.filter(([accountID, participant]) => { if (Number(accountID) === currentUserAccountID) { return false; } if (participant.hidden) { return false; } return true; });
Object.keys + filtering
let participantsIDs = Object.keys(participants).map(Number); const currentUserAccountID = 13; participantsIDs.filter((accountID) => { if (accountID === currentUserAccountID) { return false; } if (participants[accountID].hidden) { return false; } return true; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.entries + filtering
Object.keys + filtering
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.entries + filtering
636651.2 Ops/sec
Object.keys + filtering
959155.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested on the provided JSON. **Benchmark Definition** The benchmark is designed to compare two approaches for accessing object properties in JavaScript: `Object.keys()` and `Object.entries()`. The test also involves filtering the results using an array method (specifically, `filter()`). **Options Compared** There are two main options being compared: 1. **`Object.keys()`**: This method returns an array of a given object's own enumerable property names. 2. **`Object.entries()`**: This method returns an array of a given object's key-value pairs. The test also involves filtering the results using an array method (specifically, `filter()`), which is not directly related to the options being compared but rather a way to preprocess the data before comparing performance. **Pros and Cons** Here are some pros and cons of each approach: * **`Object.keys()`**: + Pros: Generally faster than `Object.entries()`, especially for large objects, since it only returns property names without values. + Cons: Does not include properties with null or undefined values. If you need to access all properties, including those with null or undefined values, use `Object.entries()` instead. * **`Object.entries()`**: + Pros: Includes all properties, regardless of their value (null, undefined, etc.). Also returns an array of arrays, which can be more convenient for certain tasks. + Cons: Generally slower than `Object.keys()` due to the additional overhead of returning key-value pairs. **Other Considerations** * The test uses a predefined object (`participants`) with varying numbers of properties. This means that the performance difference between `Object.keys()` and `Object.entries()` may vary depending on the size of the input data. * The filtering step (using `filter()`) is not directly related to the options being compared but rather a way to preprocess the data before comparing performance. **Library Used** There doesn't appear to be any external libraries used in this benchmark. However, some JavaScript engines or browsers might have internal optimizations that affect performance, such as caching or parallel processing. **Special JS Feature/Syntax** None of the code snippets provided use any special JavaScript features or syntax that would require explanation beyond the basic concepts mentioned above. **Alternatives** If you're looking for alternative approaches to accessing object properties in JavaScript, here are a few options: * **`for...in` loop**: While not as fast as `Object.keys()` or `Object.entries()`, using a `for...in` loop can provide more flexibility when working with objects that have non-enumerable properties. * **`Symbol` keys**: If you need to access properties by name, using symbols (e.g., `Object.getOwnPropertySymbols()`) can provide a way to do so. However, this requires a modern JavaScript engine and might not be supported in older browsers. Keep in mind that these alternatives might have different performance characteristics and trade-offs depending on your specific use case.
Related benchmarks:
key exists: key in object vs !!object[key] vs object[key]
Lodash.isEqual vs JSON.stringify Equality Comparison for deeply nested objects.
Object.fromEntries + Object.entries vs Object.keys
Object.keys vs Object.entries vs Object.values
object.keys() vs JSON.stringify()
Comments
Confirm delete:
Do you really want to delete benchmark?