<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
function capitalize(str) {
return str.replace(/(^[a-z]|\s[a-z])/g, text => text.toUpperCase())
}
for ( let i = 0; i < 100000; i++) {
capitalize('ho hiep 111')
}
for ( let i = 0; i < 100000; i++) {
_.capitalize('ho hiep 111')
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Regex replace | |
lodash |
Test name | Executions per second |
---|---|
Regex replace | 24.5 Ops/sec |
lodash | 174.4 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The benchmark in question compares two approaches for capitalizing a string: using Lodash's capitalize
function versus a regular expression-based approach.
Options compared
The two options being compared are:
capitalize
function: This is a utility function provided by the popular JavaScript library Lodash. It takes a string as input and returns the first character of the string in uppercase, followed by the rest of the characters in lowercase.Pros and cons
capitalize
function:Library: Lodash
Lodash is a popular JavaScript library that provides a wide range of utility functions for tasks such as array manipulation, string manipulation, and more. The capitalize
function is just one example of the many useful functions available in Lodash.
Special JS feature or syntax: None
There are no special JavaScript features or syntaxes used in this benchmark.
Other alternatives
If you want to explore other options for capitalizing a string, you could consider using:
However, these alternatives are not being compared in this specific benchmark, as they do not fit into the scope of the comparison between Lodash's capitalize
function and the regular expression-based approach.