<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
var data = [];
for (var i = 0; i < 10000; i++) {
data.push({ counter: i });
}
function isOdd(num) {
return num % 2 === 1;
}
function square(num) {
return num * num;
}
function lessThanThreeDigits(num) {
return num.toString().length < 3;
}
var expected = data.map(o => o.counter).filter(isOdd).map(square).filter(lessThanThreeDigits);
var result = _.chain(data)
.map('counter')
.filter(isOdd)
.map(square)
.filter(lessThanThreeDigits)
.value();
var result = _.reduce(data, function(acc, d) {
var c = d.counter;
return (isOdd(c) && lessThanThreeDigits(square(c)))
? [acc, square(c)]
: acc;
}, []);
var result = R.filter(lessThanThreeDigits,
R.map(square,
R.filter(isOdd,
R.pluck('counter', data))));
var result = R.pipe(
R.pluck('counter'),
R.filter(isOdd),
R.map(square),
R.filter(lessThanThreeDigits)
)(data);
const xf = R.compose(
R.map(R.prop('counter')),
R.filter(isOdd),
R.map(square),
R.filter(lessThanThreeDigits)
);
const result = R.transduce(xf, R.flip(R.append), [], data);
var result = data.map(o => o.counter).filter(isOdd).map(square).filter(lessThanThreeDigits);
var result = data.reduce((acc, d) => {
var c = d.counter;
return (isOdd(c) && lessThanThreeDigits(square(c)))
? [acc, square(c)]
: acc;
}, []);
var result = new Array();
for (var i = 0; i < data.length; i++) {
var c = data[i].counter;
if (isOdd(c)) {
var r = square(c);
if (lessThanThreeDigits(r)) {
result.push(r);
}
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash funcional style (idiomatic) | |
Lodash functional style (performance optimized with spread) | |
Ramda without relying on currying or composition | |
Ramda with currying and composition | |
Ramda with transducers | |
VanillaJS funcional style (idiomatic) | |
VanillaJS functional style (performance optimized with spread) | |
VanillaJS procedural |
Test name | Executions per second |
---|---|
Lodash funcional style (idiomatic) | 2907.0 Ops/sec |
Lodash functional style (performance optimized with spread) | 5210.2 Ops/sec |
Ramda without relying on currying or composition | 880.6 Ops/sec |
Ramda with currying and composition | 864.6 Ops/sec |
Ramda with transducers | 759.1 Ops/sec |
VanillaJS funcional style (idiomatic) | 2703.3 Ops/sec |
VanillaJS functional style (performance optimized with spread) | 5908.4 Ops/sec |
VanillaJS procedural | 6313.7 Ops/sec |
The benchmark compares the performance of various JavaScript approaches to handling data manipulation tasks, specifically focusing on extracting and processing the counter
property from a list of objects. The overall task consists of filtering out odd numbers, squaring them, and then filtering the results to retain only those values with less than two digits.
Lodash Functional Style (Idiomatic)
Lodash Functional Style (Performance Optimized with Spread)
Ramda without Relying on Currying or Composition
Ramda with Currying and Composition
Ramda with Transducers
VanillaJS Functional Style (Idiomatic)
map
, filter
, and avoids any external libraries.VanillaJS Functional Style (Performance Optimized with Spread)
VanillaJS Procedural
Lodash: A JavaScript utility library that provides functions for common programming tasks. It helps simplify coding with functions for array manipulation, object manipulation, and other aspects of JavaScript programming using a functional style.
Ramda: A functional programming library in JavaScript designed for a functional programming style, promoting immutability and side-effect-free functions. It provides functions like map
, filter
, and compose
that enable developers to create more predictable and maintainable code.
Performance: When choosing between these options, it is essential to consider the trade-off between readability and performance. In scenarios where performance is critical (e.g., with large datasets), it may be beneficial to prioritize more optimized approaches, even at the cost of some readability.
Maintainability: Code events that are easily understandable are typically easier to maintain. For teams communicating frequently and changing code, more conventional methods might be preferred.
RxJS: If the operations are tied to streams of data or events, using a reactive programming library like RxJS could offer powerful alternatives for managing and transforming data.
Immutable.js: For cases requiring immutability, Immutable.js can provide structures that enhance performance while reducing side effects commonly associated with mutable data.
Functional Libraries (e.g., folktale): Other functional programming libraries that specialize in side-effect-free functions could also be considered for developers wanting to explore beyond Lodash or Ramda.
In summary, developers can select from several approaches based on their functional programming proficiency, need for performance, and personal or team coding style preferences while weighing the pros and cons of each method.