const pathMap = new Map()
pathMap.set('test', 'site/app')
pathMap.set('vendor', 'site/vendor')
window.pathMap = pathMap
window.testId = 'test/lib/alpaca/js/alpaca'
const pathMap = window.pathMap
const parts = window.testId.split('/')
let modified
for (let { length } = parts; length > 0; --length) {
const parent = parts.slice(0, length).join('/')
const path = pathMap.get(parent)
if (path) {
parts.splice(0, length, path)
modified = true
break
}
}
const mapped = parts.join('/')
const pathMap = window.pathMap
let mapped = window.testId
for (let separator = mapped.length;;) {
const parent = mapped.substr(0, separator)
const path = pathMap.get(parent)
if (path) {
mapped = `${path}${mapped.substr(separator)}`
break
}
separator = mapped.lastIndexOf('/', separator - 1)
if (separator < 0) break
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
path part splitting and joining | |
looking up path separators |
Test name | Executions per second |
---|---|
path part splitting and joining | 583397.5 Ops/sec |
looking up path separators | 1391124.4 Ops/sec |
Let's break down the provided benchmark test cases.
Benchmark Definition
The benchmark measures how efficiently JavaScript can process paths in two different ways: by splitting and concatenating path parts, or by looking up separators in the path.
Options Compared
There are two approaches being compared:
/
) in the path and using it to look up a corresponding value in the pathMap
.Pros and Cons
pathMap
to be implemented and optimized, which can add complexity.Library
The pathMap
is a custom object that maps path prefixes to corresponding values. Its purpose is to provide a lookup mechanism for efficiently resolving paths.
Test Case Interpretation
For the first test case ("path part splitting and joining"), the benchmark measures how long it takes to:
testId
path into individual parts using /
as the separator.pathMap
.For the second test case ("looking up path separators"), the benchmark measures how long it takes to:
mapped
path and use it to look up a corresponding value in pathMap
.mapped
by concatenating the found value with the remaining part of the path.Special JS Feature/Syntax
There is no special JavaScript feature or syntax being tested in this benchmark. The tests are focused on the efficiency of the two different approaches for processing paths.
Other Alternatives
If you wanted to test alternative approaches for processing paths, some possible options could include: