Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash vs Javascript Regexp Intersection (Fix)
(version: 1)
Comparing performance of:
Javascript Regexp Intersection vs Lodash Intersection
Created:
3 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var first = _.range(0, 10000, 4); var second = _.range(0, 10000, 2);
Tests:
Javascript Regexp Intersection
const firstRegex = new RegExp(`^(${first.join('|')})$`, "g") second.filter((s) => firstRegex.test(s))
Lodash Intersection
_.intersection(first, second)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Javascript Regexp Intersection
Lodash Intersection
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON to understand what's being tested and the pros and cons of different approaches. **Benchmark Definition** The benchmark is comparing two functions: `_.intersection` from Lodash library and `filter` with regular expressions (`firstRegex.test`) on an array of numbers. **Script Preparation Code** ```javascript var first = _.range(0, 10000, 4); var second = _.range(0, 10000, 2); ``` This code creates two arrays: `first` and `second`. The `_range` function generates an array of numbers with the specified step size. **Html Preparation Code** ```html <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script> ``` This line includes the Lodash library, which provides the `_.intersection` function. **Individual Test Cases** There are two test cases: 1. **Javascript Regexp Intersection** ```javascript const firstRegex = new RegExp(`^(${first.join('|')})$`, "g"); second.filter((s) => firstRegex.test(s)); ``` This code creates a regular expression (`firstRegex`) that matches any number in the `first` array, and then filters the `second` array to only include numbers that match this regex. 2. **Lodash Intersection** ```javascript _.intersection(first, second); ``` This code uses Lodash's `_.intersection` function to find the intersection of the two arrays. **Library** The library used is Lodash, which provides a set of utility functions for working with arrays and other data structures. **Pros and Cons** 1. **Regular Expressions (Regex)**: * Pros: can be flexible and powerful for matching complex patterns. * Cons: can be slow for large datasets due to the overhead of creating and compiling regex objects. 2. **Lodash's `_intersection` function**: * Pros: optimized for performance and provides a simple, concise way to find intersections. * Cons: may not be as flexible as using regular expressions directly. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax used in this benchmark that requires explanation. **Other Alternatives** 1. **Using `filter` with `every`**: Instead of using a regex, you could use the `every` method to filter the second array: ```javascript second.filter((s) => first.every((f) => f === s)); ``` This approach can be faster than using regular expressions, but may not provide the same level of flexibility. 2. **Using `Set` data structure**: You could convert both arrays to sets and use the `intersection` method to find their intersection: ```javascript const firstSet = new Set(first); const secondSet = new Set(second); const intersection = [...firstSet].filter((x) => secondSet.has(x)); ``` This approach can be faster than using Lodash's `_intersection` function, but may require more memory. Overall, the benchmark highlights the trade-offs between using regular expressions, optimized library functions (like Lodash's `_intersection`), and other approaches for finding intersections in large datasets.
Related benchmarks:
Lodash Intersection vs. ES6 includes
Lodash vs. Set Intersection foo123
Lodash vs. Set Intersection (1000000)
Lodash vs Javascript Regexp Intersection
Comments
Confirm delete:
Do you really want to delete benchmark?