Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Native filter VS Lodash omit VS custom (map & forEach)
(version: 0)
Comparing Native filter VS Lodash omit (2 ways)
Comparing performance of:
Native filter vs Lodash omit vs Lodash omit with function vs Map based vs forEach case
Created:
6 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Script Preparation code:
var data = []; var omitedValues = []; var mappedValues = []; var dataObject = {}; var max = 1000000; var limit = max / 2; for (let i = 0; i < max; i ++) { data.push({ id: i, name: `item_${i}` }); dataObject[i] = { id: i, name: `item_${i}`}; if (i < limit) { omitedValues.push(i.toString()); } } const filter = data.filt
Tests:
Native filter
const filter = data.filter(item => { const name_index = item.name.split('_')[1]; return parseInt(name_index, 10) < limit; });
Lodash omit
const omitData = _.omit(dataObject, omitedValues);
Lodash omit with function
const omitByData = _.omit(data, (value, key) => { const name_index = value.name.split('_')[1]; return parseInt(name_index, 10) > limit; });
Map based
data.map(item =>{ const name_index = item.name.split('_')[1]; if (parseInt(name_index, 10) < limit) { mappedValues.push(item); } });
forEach case
data.forEach(item =>{ const name_index = item.name.split('_')[1]; if (parseInt(name_index, 10) < limit) { mappedValues.push(item); } });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Native filter
Lodash omit
Lodash omit with function
Map based
forEach case
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 and explain what is being tested. **Benchmark Definition** The benchmark definition compares four approaches to filter out values from an array: 1. **Native filter**: Using the `filter()` method with a callback function that returns a boolean value indicating whether the element should be included or not. 2. **Lodash omit (2 ways)**: Using two versions of Lodash's `omit` function: * Version 1: Without a callback function, which omits values that are present in the `omitedValues` array. * Version 2: With a callback function that takes two arguments (the value and key) and returns a boolean value indicating whether the value should be omitted or not. In this case, it's used to omit values where the name index is greater than or equal to the limit. 3. **Map based**: Using the `map()` method to create a new array with only the elements that meet the condition (i.e., name index less than the limit). 4. **forEach case**: Using the `forEach()` method to iterate over the array and push elements to a separate array if they meet the condition. **Library: Lodash** Lodash is a popular JavaScript utility library that provides various functions for tasks such as string manipulation, array and object manipulation, and more. In this benchmark, Lodash's `omit` function is used to filter out values from an array based on the presence of specific values in another array. **Special JS Feature/Syntax** None mentioned explicitly. **Options Compared** The benchmark compares four approaches: * Native filter: Using the `filter()` method with a callback function. * Lodash omit (2 ways): Using two versions of Lodash's `omit` function, one without a callback and one with a callback. * Map based: Using the `map()` method to create a new array. * forEach case: Using the `forEach()` method to iterate over the array. **Pros and Cons** Here are some pros and cons for each approach: 1. **Native filter**: * Pros: Lightweight, easy to implement, and fast since it only requires a single method call. * Cons: May not be as efficient as other approaches if the input array is large or complex. 2. **Lodash omit (with callback)**: * Pros: Easy to use, well-tested, and widely supported. * Cons: Requires Lodash library to be included in the project, which may add unnecessary overhead. 3. **Map based**: * Pros: Can be more efficient than filter or forEach since it creates a new array with only the desired elements. * Cons: May require additional memory allocation and copying of data. 4. **forEach case**: * Pros: Easy to implement and understand, especially for developers familiar with `forEach()` method. * Cons: May be slower than other approaches since it requires iterating over the entire array. **Other Alternatives** Some other alternatives that could be used instead of these four approaches include: * Using a library like `filter-array` or `lodash.filter` which provides more optimized filter functions. * Implementing a custom filter function using bitwise operators or other optimization techniques. * Using `Array.prototype.reduce()` method to create a new array with only the desired elements. It's worth noting that the choice of approach depends on the specific use case, performance requirements, and personal preference.
Related benchmarks:
Filter-Map: Lodash vs Native
Filter-Map: Lodash chain vs Native
Filter-Map: Lodash vs Native (smaller array
Filter: Lodash vs Native - same filter
Lodash.filter vs Native.filter
Comments
Confirm delete:
Do you really want to delete benchmark?