<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
var text = "banana"
text.charAt(0).toUpperCase() + text.slice(1)
_.upperFirst(text)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Native | |
Lodash |
Test name | Executions per second |
---|---|
Native | 3165682432.0 Ops/sec |
Lodash | 43151268.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
What is being tested?
The benchmark compares the performance of two approaches:
toUpperCase()
method to uppercase the first letter of the text, followed by slicing the rest of the string (text.slice(1)
).upperFirst
function: This is a utility function from the Lodash library that takes a string as input and returns the string with the first character uppercased.Options being compared
The benchmark compares the performance of these two approaches on the same text ("banana").
Pros and Cons:
upperFirst
function:Other considerations
The benchmark uses Chrome 126 on a Macintosh (Intel) platform with Safari 537.36 as the rendering engine. This ensures that any performance differences are specific to JavaScript execution, rather than being influenced by browser-specific optimizations or differences in rendering engines.
Library usage
Lodash is used in this benchmark as a third-party library for its upperFirst
function. Lodash provides a wide range of utility functions for tasks like string manipulation, array manipulation, and more. In this case, the upperFirst
function is specifically designed to uppercase the first letter of a string.
Special JavaScript feature or syntax
There are no specific features or syntaxes being tested in this benchmark that require special handling or explanation.
Other alternatives
If you were to create an alternative benchmark, you might consider using:
Keep in mind that each of these alternatives would require modifications to the benchmark script and setup.