Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Native loop vs lodash map & filter
(version: 0)
Comparing performance of:
Native loop vs Lodash map
Created:
4 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 SRC = [ [0, 1, 0], [0, 2, 3], [0, 0, 4], ]; function native(mask) { var offsetBlocks = []; for (let nRow = 0; nRow < mask.length; nRow++) { const row = mask[nRow]; for (let nCol = 0; nCol < row.length; nCol++) { const identifier = row[nCol]; if (identifier !== 0) { offsetBlocks.push([nCol, nRow, 0, identifier]); } } } return offsetBlocks; } function lodash(mask) { var offsetBlocks = []; offsetBlocks = _.flatMap(mask, (row, nRow) => { return _.map(row, (identifier, nCol) => { return identifier !== 0 ? [nCol, nRow, 0, identifier] : null; }); }).filter((block) => !_.isNull(block)); return offsetBlocks; }
Tests:
Native loop
native(SRC);
Lodash map
lodash(SRC);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Native loop
Lodash map
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 benchmark and explain what's being tested, compared, and discussed. **Benchmark Purpose** The primary goal of this benchmark is to compare the performance of two approaches: native JavaScript loops versus using the Lodash library for filtering and mapping data. The test case uses a predefined source array (`SRC`) as input for both functions. **Approach Comparison** The benchmark tests two options: 1. **Native loop**: This approach involves writing custom JavaScript code that iterates through the `SRC` array, identifying non-zero values in each row and pushing relevant blocks into an output array. 2. **Lodash map & filter**: This approach uses the Lodash library to perform filtering and mapping operations on the `SRC` array. Specifically, it utilizes the `_flatMap` and `_map` functions from Lodash to process the data. **Pros and Cons of Each Approach** * **Native loop**: + Pros: Customizable, potentially more efficient for small datasets. + Cons: Requires manual iteration, can be prone to errors if not implemented correctly. * **Lodash map & filter**: + Pros: Convenient, well-tested library functions reduce the risk of mistakes. + Cons: Adds overhead due to external dependencies, may perform worse on very large datasets. **Library and Syntax** In this benchmark, Lodash is used for its `_flatMap` and `_map` functions. These functions simplify the filtering and mapping process, making it easier to write efficient code without manually iterating through data. **Special JavaScript Feature/Syntax (None)** There are no specific JavaScript features or syntaxes being tested in this benchmark. **Other Alternatives** Other alternatives for similar operations might include: * Using built-in JavaScript array methods like `map()` and `filter()`, which can be more efficient than custom loops. * Employing libraries like Ramda, which provides a functional programming style for array manipulation. * Implementing the native loop using other languages or libraries, such as C++ or Julia, to take advantage of potential performance improvements. **Benchmark Preparation Code** The provided `Script Preparation Code` sets up a variable `SRC` with an example array, while the `Html Preparation Code` includes the Lodash library via a CDN. The individual test cases (`Native loop` and `Lodash map`) execute these functions using the specified benchmarks. **Latest Benchmark Result** The latest benchmark results show the performance of each approach: native loops executing at approximately 6 million executions per second, while Lodash map & filter executes at around 487,000 executions per second.
Related benchmarks:
three lodash filter vs one native map
flatMap() vs filter().map() vs for loop accumulator
flatMap() vs map().filter() v2
Lodash Object/Array map vs native
flatMap (that filters) and forEach with (conditional) push
Comments
Confirm delete:
Do you really want to delete benchmark?