<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
var fp = _.noConflict();
var context = {
eventline: { hello: 'you' },
config: 'sadas',
};
var isString = fp.isString;
var isArray = fp.isArray;
var getDefaultProps = fp.flow(
fp.filter(
_.overEvery([
isString,
function (prop) {
return _.hasIn(context, prop);
},
]),
),
fp.reduce(function (acc, i) {
acc[i] = context[i];
return acc;
}, {}),
);
var testNative = function(param) {
param.filter(myVar => (typeof myVar === 'string' || myVar instanceof String) && context[myVar]).reduce(function (acc, i) {
acc[i] = context[i];
return acc;
}, {})
}
var testNativeDash = function(param) {
param.filter(myVar => _.isString(myVar) && _.hasIn(context, myVar)).reduce(function (acc, i) {
acc[i] = context[i];
return acc;
}, {})
}
var isStr = myVar => typeof myVar === 'string' || myVar instanceof String;
var isWithinObj = myVar => !!context[myVar];
var reducer = function (acc, i) {
acc[i] = context[i];
return acc;
}
var testNativeWithFunc = function(param) {
param.filter(myVar => isStr(myVar) && isWithinObj(myVar)).reduce(reducer, {})
}
var test1 = ['eventline']
getDefaultProps(test1);
testNative(test1)
testNativeDash(test1)
testNativeWithFunc(test1)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash fp | |
native | |
native with lodash | |
native with named funcs |
Test name | Executions per second |
---|---|
lodash fp | 1003309.2 Ops/sec |
native | 17580634.0 Ops/sec |
native with lodash | 4565358.0 Ops/sec |
native with named funcs | 12890917.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Definition JSON
The benchmark definition defines four test cases, each testing a different approach to filter and transform an object. The main differences between these approaches lie in how they handle string properties and how they define functions for filtering and transforming.
filter()
and reduce()
methods, along with the typeof
operator to check if a value is a string. It defines two helper functions, isStr
and reducer
, which are used inside the filter()
callback.isStr
) and transforming (reducer
).isString
and hasIn
functions to filter and transform the object. It defines two helper functions, isStr
and reducer
, which are used inside the filter()
callback.flow
and reduceBy
, to filter and transform the object.Pros and Cons of each approach
Library usage
The Lodash library is used in two test cases:
isString
and hasIn
functions from the Lodash library.flow
and reduceBy
utilities from the Lodash fp library.Special JS features or syntax
The benchmark uses the following special JavaScript features or syntax:
myVar => typeof myVar === 'string'
)Other alternatives
For those interested in alternative approaches, here are a few options:
Object.entries()
and Array.prototype.filter()
could provide similar performance to the native approach.These alternatives are not directly related to the specific question of how different approaches handle string properties and function definitions, but they provide additional perspectives for developers interested in exploring various JavaScript implementation options.