<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var testArr = "JRQ, SCTS, KKC, KKC, KKC, RGC, PDA, PDA, RGC, KKC, PDA, PDA, PDA, SCTS, KKC, DAH, JRM, JRM, RGC, RGC, DAMB, DAMB, KKC, SCTS, RGC, PAC, KKC, KKC, SCTS, SCTS, SCTS, SCTS, KKC, KKC, MIDK, MIDK, KKC, RGC, CHTS, CHTS, CHTS, CHTS, JRQ, JRQ, SCTS, DAH, DAH, AEWC, DQQL, PAC, DQQL, DQQL, AEWC, KIS, AEWC, DQQL, AEWC, PAC, RHN, RHN, KIS, KIS, RHN, RHN, RHN, KIS, KIS, KIS, KIS, KIS, JRQ, RGC, SCTS, RGC, SCTS, KKC, SCTS, SCTS, SCTS, SCTS, SCTS, SCTS, SCTS, SCTS, SCTS, SCTS, DGJ, DGJ, RGC, KKC, KKC, MIDK, MIDK, RGC, KKC, RGC, RGC, RGC, RGC, DGJ, DGJ, PDA, RGC, JRQ, PDA, SCTS, RGC, RGC, SCTS, DXJM, JRQ, PDA, PDA, PDA, SCTS, DAH, DAH, RGC, RGC,".split(', ');
_.chain( testArr ).uniq().compact().sort().value()
Array.from(new Set( testArr.filter( Boolean ).sort() ) )
[new Set( testArr.filter( Boolean ).sort()) ]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
Native | |
Native Spread |
Test name | Executions per second |
---|---|
Lodash | 277369.6 Ops/sec |
Native | 137347.1 Ops/sec |
Native Spread | 136995.4 Ops/sec |
I'll break down the benchmark and explain what's being tested, compared, and their pros and cons.
Benchmark Definition:
The benchmark is designed to test how quickly an array can be processed from raw string data using two different approaches:
_.chain
, uniq
, compact
, sort
, value
) on the input array.Array.from
, new Set
, filter
, Boolean
, sort
) without any external libraries.Options Comparison:
The two options are:
Library: Lodash
Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, object iteration, and function composition. In this benchmark, Lodash is used to chain methods on the input array to achieve the desired result.
Pros:
Cons:
Native JavaScript Functions
The native functions used in this benchmark are:
Array.from
: Creates a new array from an iterable (in this case, the set created by filtering and sorting the input array).new Set
: Creates a new set from an iterable (in this case, the filtered and sorted input array).filter
and Boolean
: Filters out falsy values from the input array.sort
: Sorts the remaining elements of the input array.Pros:
Cons:
Other Alternatives
If you're looking for alternative approaches, consider the following:
reduce()
: Instead of chaining methods or using Array.from
and new Set
, you can use reduce()
to iterate over the array and build the result.Test Case Explanation
The test cases are:
chain
function to chain methods on the input array, resulting in the desired output.Both approaches should produce the same output, but with different syntax and potential performance characteristics.