Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Branching if-else-if, if-return, lodash cond v2
(version: 0)
Comparing performance of:
initial vs plain vs lodash
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var TYPES = { A: 'a', B: 'b', C: 'c', CExt: 'c_ext', D: 'd', E: 'e', F: 'f', G: 'g', Other: 'Other', }; function isA(x) { return x.type === 'a'; } function isB(x) { return x.type === 'b'; } function isC(x) { return x.type === 'c'; } function isExt(x) { return x.type.indexOf('_ext') >= 0; } function isD(x) { return x.type === 'd'; } function isE(x) { return x.type === 'e'; } function isF(x) { return x.type === 'f'; } function isG(x) { return x.type === 'g'; } var testData = [{ type: 'a'}, { type: 'c'}, { type: 'c_ext'}, { type: 'z'}, { type: 'f'}];
Tests:
initial
function getType(x) { if (isA(x)) { return TYPES.A; } else if (isB(x)) { return TYPES.B; } else if(isC(x)) { if (isExt(x)) { return TYPES.CExt; } return TYPES.C; } else if(isD(x)) { return TYPES.D; } else if(isE(x)){ return TYPES.E; } else if(isF(x)) { return TYPES.F; } else if(isG(x)) { return TYPES.G; } return TYPES.Other; } testData.map(getType);
plain
function getTypePlain(x) { if (isA(x)) { return TYPES.A; } if (isB(x)) { return TYPES.B; } if(isC(x)) { if (isExt(x)) { return TYPES.CExt; } return TYPES.C; } if(isD(x)) { return TYPES.D; } if(isE(x)){ return TYPES.E; } if(isF(x)) { return TYPES.F; } if(isG(x)) { return TYPES.G; } return TYPES.Other; } testData.map(getTypePlain);
lodash
var _getType = _.cond([ [isA, _.constant(TYPES.A)], [isB, _.constant(TYPES.B)], [isC, _.cond([ [isExt, _.constant(TYPES.Ext)], [_.stubTrue, _.constant(TYPES.C)] ])], [isD, _.constant(TYPES.D)], [isE, _.constant(TYPES.E)], [isF, _.constant(TYPES.F)], [isG, _.constant(TYPES.G)], [_.stubTrue, _.constant(TYPES.Other)], ]); testData.map(_getType);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
initial
plain
lodash
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Browser/OS:
Chrome 141 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
initial
51734124.0 Ops/sec
plain
52147044.0 Ops/sec
lodash
1637725.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll do my best to explain the benchmark. **Overview** The benchmark tests three different ways of implementing an `getType` function in JavaScript, which takes an object `x` and returns its corresponding type from an enumeration `TYPES`. **Benchmark Definition** The benchmark definition provides a script preparation code that defines the `TYPES` enumeration and several functions (`isA`, `isB`, etc.) that will be used to determine the type of an object. It also defines a test data array `testData` containing objects with different types. **Test Cases** There are three test cases: 1. **"initial"`**: This implementation uses a traditional if-else chain, similar to the plain implementation. 2. **"plain"`**: This implementation is identical to the initial one, but with fewer lines of code (by removing unnecessary indentation and semicolons). 3. **"lodash"`**: This implementation uses the `lodash` library's `cond` function to implement the `getType` function. **Comparison** The benchmark compares the performance of these three implementations: * **Traditional if-else chain**: A straightforward, readable, but potentially slower approach. * **Plain implementation**: Minimized code without unnecessary whitespace or semicolons. This might be a good compromise between readability and performance. * **Lodash implementation**: Uses the `cond` function to create a more concise and expressive solution. **Pros and Cons** * **Traditional if-else chain**: * Pros: Easy to read and understand, no additional dependencies required. * Cons: May be slower due to repeated checks. * **Plain implementation**: * Pros: Smaller code size without unnecessary whitespace or semicolons. * Cons: Might not be as readable for complex logic. * **Lodash implementation**: * Pros: More concise and expressive, takes advantage of `cond` function's capabilities. * Cons: Requires the `lodash` library to be loaded. **Other Considerations** The benchmark is likely designed to measure performance differences between these three implementations. Other factors that might affect performance include: * **Data size**: The size and complexity of the test data array can impact performance. * **Browser version and platform**: Different browsers may optimize JavaScript engines differently, affecting performance. * **Memory constraints**: If memory is limited, a more compact implementation (like `plain`) might be preferred. **Alternatives** Other ways to implement the `getType` function could include: * Using a switch statement instead of if-else chains * Implementing a hash table or dictionary to store type mappings for fast lookups * Using a higher-order function (e.g., `Array.prototype.map`) to create a more functional solution However, these alternatives are not currently represented in the benchmark.
Related benchmarks:
lodash isNil vs native isNil
Lodash isUndefined vs isNil
lodash isNil vs ! Operator
lodash isNil vs native isNil with if
lodash isNil vs === null || === undefined
Comments
Confirm delete:
Do you really want to delete benchmark?