Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Capitalize
(version: 0)
Comparing performance of:
1 vs 2 vs 3 vs 4 vs 5
Created:
9 years ago
by:
Guest
Go to the latest result
Tests:
1
function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); } capitalizeFirstLetter('string');
2
function capitalizeFirstLetter(string) { return string.replace(/^./, string[0].toUpperCase()); } capitalizeFirstLetter('string');
3
function capitalizeFirstLetter(string) { return string[0].toUpperCase() + string.slice(1); } capitalizeFirstLetter('string');
4
String.prototype.capitalizeFirstLetter = function() { return this.charAt(0).toUpperCase() + this.slice(1); } 'string'.capitalizeFirstLetter();
5
String.prototype.capitalizeFirstLetter = function() { return this[0].toUpperCase() + this.slice(1); } 'string'.capitalizeFirstLetter();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
1
2
3
4
5
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
3
83.3M/s
1
81.7M/s
2
18.1M/s
5
9.2M/s
4
9.0M/s
View exact numbers
Test name
Executions per second
✓
3
83,302,584 Ops/sec
1
81,682,360 Ops/sec
2
18,127,278 Ops/sec
5
9,151,775 Ops/sec
4
9,009,189 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll do my best to break down the provided benchmark and its test cases. **Benchmark Overview** The benchmark, named "Capitalize", measures the performance of different approaches to capitalize the first letter of a string in JavaScript. There are five test cases: 1. A standalone function `capitalizeFirstLetter` that takes a string as input. 2. A modified version of the same function with an additional regular expression-based approach. 3. Another version of the function, identical to the first one. 4. A method on the `String.prototype` object that capitalizes the first letter of a string. 5. Another version of the method on `String.prototype`, slightly different from the previous one. **Options Compared** The benchmark compares two approaches: 1. **Standalone Function**: The first test case uses a standalone function, where the implementation is self-contained and not dependent on any external object or prototype methods. 2. **Prototype Method**: The last three test cases use a method on the `String.prototype` object, which allows for more concise code but may incur overhead due to prototype pollution concerns. **Pros and Cons** 1. **Standalone Function** * Pros: + Easier to understand and maintain, as it's self-contained. + Less likely to pollute the global scope or affect other parts of the codebase. * Cons: + May have additional overhead due to function creation and invocation. 2. **Prototype Method** * Pros: + More concise and expressive code, which can be beneficial for readability. * Cons: + May pollute the global scope or affect other parts of the codebase if not used carefully. **Library and Special JS Features** None of the test cases use any external libraries or special JavaScript features beyond what is built into the language. The only notable aspect is the use of `String.prototype`, which allows for a more functional programming style but requires careful consideration to avoid polluting the prototype chain. **Alternative Approaches** Other possible approaches to capitalizing the first letter of a string in JavaScript could include: 1. Using `Array.prototype.map()` and `String.prototype.charAt()`. 2. Employing a custom `toUpperCase()` function for strings. 3. Utilizing a library or framework that provides such a method, if not already included. These alternatives are not tested by the provided benchmark, but they demonstrate other ways to approach this common string manipulation task in JavaScript.
Related benchmarks
Capitalize
Capitalize
lodash vs own capitalize function
Comments
Confirm delete:
Do you really want to delete benchmark?