const parseConsumerDisplayName = (firstName, middleName, lastName) => {
const fullName = [lastName, firstName, middleName]
.filter(Boolean)
.join(' ')
.substr(0, 26);
const givenNames = fullName.substring(lastName.length, fullName.length);
return [givenNames, lastName].join(' ');
};
parseConsumerDisplayName('Test', 'M', 'Name');
parseConsumerDisplayName('ReallyReallyLongFirstName', 'M', 'AbsurdlyLongLastName');
const parseConsumerDisplayName = (firstName, middleName, lastName) => {
const lastNameLength = lastName.length;
const givenNames = [firstName, middleName].filter(Boolean).join(' ').substr(0, 26-lastNameLength);
return [givenNames, lastName].join(' ');
};
parseConsumerDisplayName('Test', 'M', 'Name');
parseConsumerDisplayName('ReallyReallyLongFirstName', 'M', 'AbsurdlyLongLastName');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Old | |
New |
Test name | Executions per second |
---|---|
Old | 759222.9 Ops/sec |
New | 887002.9 Ops/sec |
Overview of the Benchmark
The provided JSON represents a JavaScript microbenchmark created on MeasureThat.net. The benchmark tests two different implementations of a function parseConsumerDisplayName
, which takes three string parameters: firstName
, middleName
, and lastName
. The goal is to determine which implementation is faster.
Test Cases
There are only two test cases:
const parseConsumerDisplayName = (firstName, middleName, lastName) => {
const fullName = [lastName, firstName, middleName]
.filter(Boolean)
.join(' ')
.substr(0, 26);
const givenNames = fullName.substring(lastName.length, fullName.length);
return [givenNames, lastName].join(' ');
};
const parseConsumerDisplayName = (firstName, middleName, lastName) => {
const lastNameLength = lastName.length;
const givenNames = [firstName, middleName]
.filter(Boolean)
.join(' ')
.substr(0, 26 - lastNameLength);
return [givenNames, lastName].join(' ');
};
Comparison of Options
The two implementations differ in the way they calculate givenNames
. The Old implementation uses a substring operation to extract givenNames
, while the New implementation uses a different approach with a subtraction-based optimization.
Pros and Cons:
Other Considerations
The benchmark uses a JavaScript engine (Chrome 85) and runs on a desktop platform with macOS 10.15.6. The parseConsumerDisplayName
function is designed to extract the first name from a full name, taking into account middle names.
Libraries and Special Features
There are no libraries used in this benchmark. However, it's worth noting that some JavaScript engines or browsers may have specific optimizations or features that could affect the performance of this code (e.g., const
propagation or String.prototype.substr()
optimization).
Alternatives
If you wanted to write a similar benchmark, you could consider using other test frameworks like JSMbench or Benchmark.js. MeasureThat.net is specifically designed for JavaScript microbenchmarks, and its UI provides a convenient way to run and compare multiple tests.
In terms of alternatives for writing the parseConsumerDisplayName
function itself, you could explore other approaches, such as: