<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash-fp/0.10.4/lodash-fp.min.js"></script>
var arr = [{id: 1, name: "Person 1"}, {id: 2, name: "Person 2"}];
var newArr = _.map(arr, function(a) {
return a.id === 1 ? {id: 1, name: "Person New Name"} : a;
});
// ES6
var newArr = arr.map(function(a) {
return a.id === 1 ? {id: 1, name: "Person New Name"} : a;
});
var index = _.findIndex(arr, {id: 1});
// Replace item at index using native splice
arr.splice(index, 1, {id: 100, name: 'Person New Name'});
const index = R.findIndex(R.propEq('id', 1))(arr)
arr.splice(index, 1, {id: 100, name: 'Person New Name'});
var newArr = R.map(function(a) {
return a.id === 1 ? {id: 1, name: "Person New Name"} : a;
})(arr);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash map | |
es6 map | |
lodash findIndex and splice | |
ramda findIndex and splice | |
ramda map |
Test name | Executions per second |
---|---|
lodash map | 800214.8 Ops/sec |
es6 map | 42949436.0 Ops/sec |
lodash findIndex and splice | 5924058.5 Ops/sec |
ramda findIndex and splice | 2106770.0 Ops/sec |
ramda map | 9059769.0 Ops/sec |
Overview of the Benchmark
The provided JSON represents a JavaScript microbenchmarking test case on MeasureThat.net, which compares the performance of different approaches for finding an item by ID in an array and replacing it with a new value.
Benchmark Definition
The benchmark definition consists of four individual test cases:
lodash map
map
lodash findIndex
and splice
ramda findIndex
and splice
These test cases demonstrate different ways to achieve the same goal using various JavaScript libraries: Lodash, Ramda, and native JavaScript.
Options Compared
The benchmark compares the performance of:
map()
and findIndex()
, along with manual indexing and splicing.Pros and Cons of Each Approach
Library Purpose
The lodash
library provides a comprehensive set of functional programming utilities, including the map()
function used in the benchmark. It's designed to make common tasks easier and more efficient.
The ramda
library offers a more concise API for functional programming, with features like higher-order functions and currying. It's intended to be a more expressive alternative to Lodash.
Special JS Features/Syntax
None of the test cases explicitly use any special JavaScript features or syntax that would require additional explanation.
Other Alternatives
If you're looking for alternatives to Lodash and Ramda, consider:
These libraries offer varying degrees of similarity to Lodash and Ramda, but may have their own strengths and weaknesses depending on your specific use case.