Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
remove undefined from object. entries.filter vs for of vs for in
(version: 0)
Comparing performance of:
filter object entries vs for of Object.entries vs for in
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const testObject = { a: 'a', b: undefined, c: 'c', d: undefined, e: undefined }
Tests:
filter object entries
const testObject = { a: 'a', b: undefined, c: 'c', d: undefined, e: undefined } Object.fromEntries(Object.entries(testObject).filter(([_, value]) => value !== undefined))
for of Object.entries
const testObject = { a: 'a', b: undefined, c: 'c', d: undefined, e: undefined } const testObjectCopy = {...testObject} for (const [prop, value] of Object.entries(testObjectCopy)) if (value === undefined) delete testObjectCopy[prop]
for in
const testObject = { a: 'a', b: undefined, c: 'c', d: undefined, e: undefined } const testObjectCopy = {...testObject} for (const prop in testObjectCopy) if (testObjectCopy.hasOwnProperty(prop) && testObjectCopy[prop] === undefined) delete testObjectCopy[prop]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
filter object entries
for of Object.entries
for in
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/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
filter object entries
1483802.0 Ops/sec
for of Object.entries
1550265.8 Ops/sec
for in
1721107.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided JSON and benchmark results in an easy-to-understand format. **Benchmark Overview** The provided benchmark measures the performance of three different approaches to remove `undefined` values from an object: using `filter()`, `for...of`, and `for...in`. The benchmark aims to determine which approach is the most efficient. **Approaches Compared** 1. **Filter()**: This approach uses the `Array.prototype.filter()` method to create a new array with only the elements that pass the test implemented by the provided function. 2. **For...of (Object.entries)**: This approach iterates over an array of key-value pairs using a `for...of` loop, filtering out the values that are equal to `undefined`. 3. **For...in**: This approach uses a traditional `for...in` loop to iterate over the object's properties, checking if each value is `undefined` and deleting it if so. **Pros and Cons of Each Approach** 1. **Filter()**: * Pros: Efficient and concise, as it creates a new array with filtered values. * Cons: Creates an extra memory allocation for the new array, which can be costly in terms of performance. 2. **For...of (Object.entries)**: * Pros: More modern and efficient than traditional `for...in`, as it avoids the overhead of `hasOwnProperty()` checks. * Cons: May require more memory to store the internal iterator object. 3. **For...in**: * Pros: Simple and easy to implement, but may be slower due to the overhead of property checking and deleting. * Cons: Less modern and less efficient than the other two approaches. **Library and Special JS Features** The benchmark uses none of its own libraries or special JavaScript features. It relies solely on built-in methods and syntax for each approach. **Other Alternatives** Some alternative approaches to remove `undefined` values from an object could include: * Using a library like Lodash or Ramda, which provide utility functions for filtering and manipulating objects. * Using a map function with a predicate callback * Using the spread operator (`{...}`) to create a new object with filtered properties Keep in mind that these alternatives may introduce additional dependencies or overhead compared to using built-in methods. **Benchmark Results** The latest benchmark results show that: 1. **For...in** is currently the fastest approach, with an average of 1483802 executions per second. 2. **Filter()** is the next fastest, with an average of 1550265 executions per second. 3. **For...of (Object.entries)** is the slowest, with an average of 1721107 executions per second. These results may vary depending on the specific use case and environment in which the benchmark is run.
Related benchmarks:
For in vs For of
undefined vs. typeof vs. in vs. operator
Object property: delete vs undefined 2
Object.entries vs Object.keys vs for...in
in vs not undefined
Comments
Confirm delete:
Do you really want to delete benchmark?