Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash xor / sets / includes perf
(version: 0)
Searching for faster solution for _.xor(a, b)
Comparing performance of:
lodash xor vs with Sets vs with ES6 filter-includes
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function dec2hex (dec) { return dec < 10 ? '0' + String(dec) : dec.toString(16) } function generateId (len) { var arr = new Uint8Array((len || 40) / 2) window.crypto.getRandomValues(arr) return Array.from(arr, dec2hex).join('') } var a = [] var b = [] for( let i = 0; i < 1000; i++) { a[i] = generateId(16) b[i] = generateId(16) }
Tests:
lodash xor
const res = _.xor(a, b)
with Sets
var as = new Set(a) var bs = new Set(b) const res = a .filter((row_id) => bs.has(row_id)) .concat(b.filter((row_id) => !as.has(row_id)))
with ES6 filter-includes
const res = a.filter(x => !b.includes(x)).concat(b.filter(x => !a.includes(x)));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash xor
with Sets
with ES6 filter-includes
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/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash xor
2294.2 Ops/sec
with Sets
4809.9 Ops/sec
with ES6 filter-includes
141.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark JSON and explain what's being tested. **Benchmark Definition** The benchmark is designed to measure the performance of different approaches for solving the `_.xor(a, b)` function from the Lodash library. However, it also includes two alternative implementations using ES6 features: `Set` objects and `filter()` with `includes()` methods. **Options compared** Three options are being compared: 1. **Lodash's original implementation (`_.xor(a, b)`)**: This is the baseline for comparison. 2. **With Sets**: Using `Set` objects to eliminate duplicates before filtering. 3. **ES6 filter-includes**: Using the `filter()` method with an arrow function that checks if a value is not included in the `b` array using `includes()`. **Pros and Cons of each approach** 1. **Lodash's original implementation (`_.xor(a, b)`)**: * Pros: Likely optimized for performance by Lodash's team. * Cons: May rely on internal implementation details or use specialized libraries like Lodash. 2. **With Sets**: Using `Set` objects to eliminate duplicates before filtering. * Pros: Efficient in terms of time complexity (O(n)), as removing duplicates from the set is faster than filtering individual elements. * Cons: Requires creating a new set, which might have memory implications for large datasets. 3. **ES6 filter-includes**: Using the `filter()` method with an arrow function that checks if a value is not included in the `b` array using `includes()`. * Pros: Easy to understand and implement, as it leverages standard JavaScript features. * Cons: Might be slower than the other approaches due to the overhead of filtering individual elements. **Library/Feature explanations** 1. **Lodash**: A popular JavaScript library providing a wide range of utility functions for tasks like array manipulation, string processing, and more. 2. **Set objects**: A built-in JavaScript data structure that stores unique values and allows efficient membership testing (i.e., checking if a value is present in the set). 3. **ES6 filter-includes**: The `filter()` method with an arrow function that uses the `includes()` method to check if a value is not included in another array. **Special JS feature/syntax** There are no special JavaScript features or syntax mentioned in the provided benchmark JSON. However, it's worth noting that some browser implementations might have specific optimizations or extensions for ES6 features like `Set` objects and arrow functions. **Alternatives** Other approaches could be explored to optimize the `_xor(a, b)` function: 1. **Using a custom set data structure**: Implementing a more efficient set data structure, such as a hash table, to reduce lookup time. 2. **Parallel processing**: Using Web Workers or other parallel processing techniques to speed up the computation by distributing the workload across multiple CPU cores. 3. **Just-In-Time (JIT) compilation**: Using a JIT compiler like V8 (used by Chrome) to optimize the generated code for better performance. Keep in mind that these alternatives might require additional development effort and might not always outperform the existing approaches.
Related benchmarks:
Array.prototype.every vs Lodash every_2
Lodash union VS ES6 Set
Is Some faster than !!find
lodash uniq vs Array.from(new Set()) vs spread new Set() vs for vs for memory optimized 4
Array.prototype.every vs Lodash every()
Comments
Confirm delete:
Do you really want to delete benchmark?