<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var values = [{ 'n': 31 }, { 'n': 32 }, { 'n': 33 }, { 'n': 1 }, { 'n': 2 }, { 'n': 3 }, { 'n': 21 }, { 'n': 22 }, { 'n': 23 }];
_.minBy(values, function(item) { return item.n; });
values.sort((a, b) => b.n - a.n)[0];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.minBy | |
native |
Test name | Executions per second |
---|---|
_.minBy | 3856706.2 Ops/sec |
native | 3215965.5 Ops/sec |
I'll explain the benchmark in detail.
What is tested:
The provided JSON represents a JavaScript microbenchmark on MeasureThat.net, which compares the performance of Lodash's minBy
function with native JavaScript implementation for sorting an array of objects based on a specific property (n
).
Options compared:
Two options are being compared:
minBy
function: This is a utility function provided by the Lodash library, which returns the first element in an array that satisfies a given condition (in this case, having the minimum value of the n
property).sort()
method and provides a compare function to sort the array based on the n
property.Pros and cons:
minBy
function:Library:
The Lodash library is used in this benchmark. Lodash provides a collection of utility functions for common tasks, such as array manipulation, string manipulation, and object manipulation. In this case, minBy
is used to find the first element in an array that satisfies a given condition.
Special JS feature/syntax:
There are no special JavaScript features or syntax being tested in this benchmark. The focus is on comparing two implementation approaches for sorting an array of objects based on a specific property.
Other alternatives:
If you want to compare other implementations, such as:
Array.prototype.reduce()
with a custom callback functionYou can modify the benchmark definition to use these alternatives.
Keep in mind that MeasureThat.net is designed to focus on JavaScript microbenchmarks, so it's likely that alternative implementations would not be included by default.