var string = `eyJ0ZXN0IjoxLCJjb21wYW5pZXMiOlsxLDIsMyw0LDUsNl0sInVzZXIiOiJ0ZXN0IiwiZGF0ZSI6Illlc3RlcmRheSIsInN0YXJ0QXQiOiIyMDIzLTExLTAxVDAwOjAwOjAwLjAwMFoifQ==`;
string.replace(/(=+)$/, '')
string.split('=')[0]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Regex | |
Split |
Test name | Executions per second |
---|---|
Regex | 5931178.0 Ops/sec |
Split | 15659519.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
What is being tested?
The provided benchmark compares two approaches to manipulate a base64 string: using regular expressions (regex) and splitting the string using the =
character as a delimiter.
In this specific test case, the base64 string is:
eyJ0ZXN0IjoxLCJjb21wYW5pZXMiOlsxLDIsMyw0LDUsNl0sInVzZXIiOiJ0ZXN0IiwiZGF0ZSI6Illlc3RlcmRheSIsInN0YXJ0QXQiOiIyMDIzLTExLTAxVDAwOjAwOjAwLjAwMFoifQ==
The test case consists of two individual tests:
replace()
method with a regex pattern to remove the last character (=
) from the base64 string.=
character as a delimiter and takes the first part.Options compared
The two approaches are compared in terms of their performance, specifically:
Pros and Cons of each approach:
Library/Dependency
None explicitly mentioned, but String.prototype.replace()
and String.prototype.split()
are built-in methods in JavaScript.
Special JS feature/syntax
None mentioned, but this benchmark focuses on basic string manipulation using native JavaScript methods.
Other alternatives
If you were to modify the test case or use a different approach, some alternatives could be:
RegExp
)lodash.string
)