<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
arr.reverse()
_.reverse(arr)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.reverse | |
lodash reverse |
Test name | Executions per second |
---|---|
Array.prototype.reverse | 17036000.0 Ops/sec |
lodash reverse | 8474951.0 Ops/sec |
Let's break down the provided benchmark definition and test cases.
Benchmark Definition Json
The website allows users to create custom benchmarks, which in this case, is comparing the performance of lodash.reverse
with Array.prototype.reverse
.
What is being tested?
In this benchmark, two approaches are compared:
lodash
library, which also offers many other useful functions for manipulating arrays.Options Compared
The two options being compared are:
Array.prototype.reverse()
method_reverse()
function from the lodash
libraryPros and Cons of Each Approach
Native Array.prototype.reverse
Pros:
Cons:
_reverse() from lodash.js
Pros:
Cons:
Library: lodash.js
The lodash
library is a popular JavaScript utility library that provides many functions for working with arrays, strings, numbers, and more. The _reverse()
function is just one example of its many utility functions.
Special JS Feature/Syntax
There isn't any special JavaScript feature or syntax being tested in this benchmark. It's purely about comparing the performance of two different approaches to reversing an array.
Other Alternatives
If you're interested in other alternatives for reversing arrays, here are a few options:
Array.prototype.slice()
and then concatenating the reversed slices: arr.slice(0).reverse().concat(arr.slice(1))
Array.prototype.map()
and Array.prototype.reverse()
: arr.map((_, i) => arr[i]).reverse()
for (var i = 0; i < arr.length / 2; i++) { arr[i] = arr[arr.length - i - 1]; }
These alternatives can be used in custom benchmarks to compare their performance with the native Array.prototype.reverse()
and _reverse()
functions from lodash.js
.