Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Uniq lodash vs native
(version: 2)
Compares uniq lodash vs doing it in native way
Comparing performance of:
lodash vs native indexOf vs native includes
Created:
6 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 arr = ['orange', 'apple', 'pie', 'brown', 'orange', 'yellow', 'orange', 'appfeel', 'pie', 'blue', 'kiwi'];
Tests:
lodash
_.uniq(arr);
native indexOf
arr.reduce((newArr, s) => { if (newArr.indexOf(s) < 0) { newArr.push(s); } return newArr; }, []);
native includes
arr.reduce((newArr, s) => { if (!newArr.includes(s)) { newArr.push(s); } return newArr; }, []);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash
native indexOf
native includes
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 is being tested. **Benchmark Definition** The benchmark compares two approaches to remove duplicates from an array: using Lodash's `uniq` function and doing it manually in JavaScript (without any external libraries). **Script Preparation Code** The script preparation code defines an array `arr` with duplicate elements, which will be used for the benchmark. ```javascript var arr = ['orange', 'apple', 'pie', 'brown', 'orange', 'yellow', 'orange', 'appfeel', 'pie', 'blue', 'kiwi']; ``` **Html Preparation Code** The HTML preparation code includes a reference to Lodash's `lodash.min.js` file, which is used by the benchmark. ```html <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script> ``` **Individual Test Cases** There are three test cases: 1. **Lodash: _.uniq(arr)** This test case uses Lodash's `uniq` function to remove duplicates from the array. 2. **Native indexOf: arr.reduce((newArr, s) => {...})** This test case manually removes duplicates by using the `indexOf` method and reducing the array iteratively. 3. **Native includes: arr.reduce((newArr, s) => {...})** This test case is similar to the previous one but uses the `includes` method instead of `indexOf`. **Library: Lodash** Lodash is a popular JavaScript library that provides various utility functions for tasks like data manipulation, string processing, and more. The `uniq` function is used to remove duplicate elements from an array while preserving the original order. In this benchmark, Lodash's `uniq` function is used as a reference implementation, and the native approaches are compared to see which one performs better. **Native JavaScript Features** The native tests use two different approaches: * `indexOf`: Returns the index of the first occurrence of an element in the array. This method is used in the first test case. * `includes`: Returns a boolean indicating whether an element is present in the array. This method is used in the second test case. These methods are built-in JavaScript functions that can be used to remove duplicates from an array. However, they have some differences: * `indexOf` returns the index of the first occurrence, while `includes` returns a boolean value. * Both methods have different performance characteristics due to their implementation details. **Other Considerations** When dealing with large datasets or performance-critical code, it's essential to consider the following factors: * Cache locality: The way elements are stored in memory can impact cache locality and performance. * Memory allocation: The amount of memory allocated for each test case can affect performance, especially when using dynamic arrays. * JavaScript engine optimizations: Modern JavaScript engines like V8 (used by Chrome) perform various optimizations to improve performance, such as inlining functions and reusing cached results. **Alternatives** Other alternatives for removing duplicates from an array include: * Using `Set` objects, which provide a more efficient way to remove duplicates without sorting the array. * Implementing a custom solution using algorithms like merge sort or quicksort. * Utilizing libraries like Ramda or Immutable.js, which offer more functional programming-style solutions for data manipulation. However, these alternatives might not be as straightforward to implement and may have different performance characteristics compared to the native approaches used in this benchmark.
Related benchmarks:
lodash uniq vs native uniq
lodash uniqBy vs custom uniqBy
uniqBy performance lodash vs native
Lodash uniqBy vs Javascript uniqBy
Comments
Confirm delete:
Do you really want to delete benchmark?