Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash union vs native set spread
(version: 0)
Comparing performance of:
lodash vs native set spread back to array
Created:
one year ago
by:
Guest
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 a = [1, 2, 3, 4, 5] var b = [3, 4, 5, 6, 7]
Tests:
lodash
var c = _.union(a,b)
native set spread back to array
var c = [...new Set([a,b])]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash
native set spread back to array
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash
4550215.0 Ops/sec
native set spread back to array
7921608.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is tested, compared, and analyzed. **Benchmark Overview** The benchmark measures the performance difference between using `_.union` from Lodash library versus creating an array from a set with the spread operator (`[...]`) in JavaScript. The test cases are designed to compare these two approaches on arrays of similar sizes. **Options Compared** Two options are compared: 1. **Lodash _.union**: This is a utility function from the Lodash library that returns a new array containing all elements from the input arrays, without duplicates. 2. **Native Set Spread Back to Array**: This approach creates a set from the input arrays using the spread operator (`[...]`), and then converts it back to an array. **Pros and Cons** Here's a brief analysis of each approach: * **Lodash _.union**: + Pros: More concise, readable code; potentially more maintainable due to the familiarity with Lodash. + Cons: Might incur overhead from loading the Lodash library; may not be optimized for performance. * **Native Set Spread Back to Array**: + Pros: Native implementation, likely optimized for performance; does not require external libraries. + Cons: May have a steeper learning curve due to the set spread operator syntax; might result in less readable code. **Library and Purpose** In this benchmark, Lodash is used as a utility library, providing the `_.union` function. The purpose of using Lodash here is to demonstrate how performance can vary when using different libraries versus native JavaScript implementations. **Special JS Feature or Syntax** The use of the set spread operator (`[...]`) is a relatively recent feature in JavaScript (introduced in ECMAScript 2015) that allows creating an array from an iterable (like a set). This syntax is concise and readable, but might not be familiar to all developers. **Other Alternatives** If you're interested in exploring other approaches or alternatives: * Using native `Array.prototype.reduce()` instead of Lodash's `_union()`. * Utilizing the `Set` data structure directly (without spreading it back into an array) for both input and output. * Comparing performance with different JavaScript engines, like SpiderMonkey, V8, or Safari. In summary, this benchmark compares two approaches to creating a union from arrays: using Lodash's `_union()` function versus the native set spread operator syntax. The pros and cons of each approach have been highlighted, as well as some potential alternatives for further exploration.
Related benchmarks:
_.union vs native Set() implementation
Lodash union vs native Set() implementation
Lodash union vs Native Javascript
Corrected Lodash _.union vs native Set()
Comments
Confirm delete:
Do you really want to delete benchmark?