Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Delete or not delete
(version: 0)
Delete speed can be frustrating on V8 engine, but is it so in our case?
Comparing performance of:
Use delete vs Use undefined vs Use filter and reduce
Created:
4 years ago
by:
Registered User
Jump to the latest result
Tests:
Use delete
const content = '0'.repeat(53000); // ~53kb var cache = {}; let i = 100; // will give us ~5Mb while (i--) { const name = `item-${i}`; cache[name] = content; } // we will delete all 100 items function invalidateItem() { return true; } const invalidatedCache = (cache) => { const updatedCache = {}; Object.assign(updatedCache, cache); Object.keys(cache).filter(invalidateItem).forEach((name) => delete updatedCache[name]); return updatedCache; }; invalidatedCache(cache);
Use undefined
const content = '0'.repeat(53000); // ~53kb var cache = {}; let i = 100; // will give us ~5Mb while (i--) { const name = `item-${i}`; cache[name] = content; } // we will delete all 100 items function invalidateItem() { return true; } const invalidatedCache = (cache) => { const updatedCache = {}; Object.assign(updatedCache, cache); Object.keys(cache).filter(invalidateItem).forEach((name) => { updatedCache[name] = undefined; }); return updatedCache; }; invalidatedCache(cache);
Use filter and reduce
const content = '0'.repeat(53000); // ~53kb var cache = {}; let i = 100; // will give us ~5Mb while (i--) { const name = `item-${i}`; cache[name] = content; } // we will delete all 100 items function invalidateItem() { return true; } const invalidatedCache = (cache) => { const updatedCache = Object.keys(cache).filter(invalidateItem).reduce((cacheClone, key) => { cacheClone[key] = cache[key]; return cacheClone; }, Object.create(null)); return updatedCache; }; invalidatedCache(cache);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Use delete
Use undefined
Use filter and reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 18_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 18 on iOS 18.3.1
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Use delete
61534.7 Ops/sec
Use undefined
66106.4 Ops/sec
Use filter and reduce
93245.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Purpose** The primary goal of this benchmark is to compare the performance of three different approaches for invalidating and pruning a cache: 1. Using the `delete` operator 2. Setting values to `undefined` 3. Using the `filter()` method with `reduce()` **Approach 1: Using `delete` operator** This approach directly deletes the key-value pair from the cache object using the `delete` operator. Pros: * Simple and straightforward implementation. * Fast and efficient, as it directly modifies the cache object. Cons: * May not be suitable for modern JavaScript engines that optimize garbage collection, potentially leading to slower performance due to the overhead of deleting individual keys. * Can lead to issues with cache coherence if other parts of the application rely on the presence or absence of specific keys in the cache. **Approach 2: Setting values to `undefined`** This approach sets the value associated with each key to `undefined`, effectively invalidating and removing the key-value pair from the cache. Pros: * More modern and efficient than using the `delete` operator, as it leverages JavaScript's garbage collection mechanisms. * Reduces the risk of cache coherence issues. Cons: * May lead to slower performance due to the overhead of creating new objects and updating references. * Can result in memory leaks if not properly handled. **Approach 3: Using `filter()` method with `reduce()`** This approach uses the `filter()` method to create a new array with only the keys that should remain in the cache, and then uses `reduce()` to update the cache object with the filtered values. Pros: * Can be more efficient than using `delete` or setting values to `undefined`, as it leverages built-in methods for filtering and reducing arrays. * Allows for more control over the caching logic, making it easier to implement custom caching strategies. Cons: * More complex implementation compared to the other two approaches. * May lead to slower performance due to the overhead of creating new arrays and objects. **Library Used** In this benchmark, no specific libraries are used. However, the `Object.assign()` method is employed to update the cache object, which is a built-in JavaScript method. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax explicitly mentioned in this benchmark. However, it's worth noting that some modern browsers and engines have introduced features like `const` declarations, `let` and `const` functions, and other optimizations that can affect performance. **Alternative Approaches** Other approaches to invalidating and pruning caches could include: 1. Using a custom cache management library or framework. 2. Employing a more advanced caching strategy, such as using a LRU (Least Recently Used) cache or an eviction policy. 3. Implementing a caching proxy or wrapper around the underlying data structure. These alternatives may offer better performance, scalability, or maintainability benefits depending on the specific use case and requirements. In conclusion, this benchmark provides a useful comparison of three approaches to invalidating and pruning caches in JavaScript, highlighting the pros and cons of each method. By understanding these trade-offs, developers can make informed decisions about their caching strategies and optimize their code for better performance.
Related benchmarks:
Testing JS Performance of deleting a large map.
Check vs try...catch
Delete vs destructure for objects in loop
Map() delete while iterating vs clear
Comments
Confirm delete:
Do you really want to delete benchmark?