Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Filter null/undefined/empty string key values from an Object
(version: 0)
Comparing performance of:
Using For Loop vs Using Filter and Reduce
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Using For Loop
const obj = { a: '', b: null, c: 'dog'}; for (let propName in obj) { if ( obj[propName] === null || obj[propName] === undefined || obj[propName] === '' ) { delete obj[propName]; } } // result: {c: 'dog'} console.log(obj)
Using Filter and Reduce
const obj = { a: '', b: null, c: 'dog'}; const newObj = Object.entries(obj) .filter(([key, value]) => !!value) .reduce((acc, [key, value]) => { acc[key] = value; return acc; }, {}); // result: {c: 'dog'} console.log(newObj);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Using For Loop
Using Filter and Reduce
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/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Using For Loop
191207.0 Ops/sec
Using Filter and Reduce
217021.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and its pros and cons. **Benchmark Overview** The benchmark measures how quickly two approaches can filter out null, undefined, or empty string values from an object. The approaches are: 1. Using a `for` loop 2. Using `Object.entries`, `filter`, and `reduce` **For Loop Approach** The first approach uses a `for` loop to iterate over the object's properties. For each property, it checks if the value is null, undefined, or an empty string. If so, it deletes the property from the object. Pros: * Easy to understand and implement * Familiar syntax for most developers Cons: * Looping over the object's properties can be slower than using native functions like `filter` and `reduce` * May involve more memory allocations and deallocations due to property deletions **Filter and Reduce Approach** The second approach uses the `Object.entries` function to get an array of key-value pairs, then applies a filter to remove null, undefined, or empty string values. The remaining pairs are reduced into a new object using `reduce`. Pros: * Native functions are optimized for performance * Can take advantage of modern JavaScript features like `for...of` loops (not used here) and `Map` objects Cons: * May be less intuitive for developers without experience with these functions * Requires more understanding of the underlying API **Library Usage** None of the provided benchmark definitions use any external libraries. **Special JS Feature/Syntax** The benchmark uses modern JavaScript features like template literals (`\r\n`) and arrow functions (`() => {}`). However, it does not use any experimental or proposed features. **Alternative Approaches** Other approaches to filter out null, undefined, or empty string values from an object might include: 1. Using `Object.fromEntries` instead of `reduce` 2. Using a regular expression with the `/^./` pattern to match non-empty strings 3. Using the `filter()` method directly on the object (although this may not be supported in older browsers) **Considerations** When choosing an approach, consider factors like: * Performance: Native functions like `filter` and `reduce` are generally faster than using a `for` loop. * Code readability: Choose an approach that is easy to understand and maintain. * Browser support: Ensure the chosen approach works in all supported browsers. In this benchmark, the `Filter and Reduce` approach appears to be the fastest, but it's essential to evaluate both approaches for your specific use case and requirements.
Related benchmarks:
abc123
reduce vs map as;dfkajdf
test filter
Reduce vs map with empty filter
remove undefined from object. entries.filter vs for of vs for in
Comments
Confirm delete:
Do you really want to delete benchmark?