Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.endsWith vs .charAt for single char
(version: 0)
Testing some things
Comparing performance of:
startsWith vs .charAt
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
var data = window.data = []; const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var TOTAL_STRINGS = window.TOTAL_STRINGS = 100000; function getRandomInt(max) { return Math.floor(Math.random() * max); } function makeRandomString(len) { var text = ""; for( var i=0; i < len; i++ ) { text += possible.charAt(getRandomInt(possible.length)); } return text; } while (data.length < TOTAL_STRINGS) { data.push(makeRandomString(getRandomInt(20))); }
Tests:
startsWith
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var regex = window.regex; while (x < TOTAL_STRINGS) { const str = data[x]; str.endsWith("9"); x += 1; }
.charAt
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; while (x < TOTAL_STRINGS) { const str = data[x]; str.charAt(TOTAL_STRINGS - 1) === '9'; x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
startsWith
.charAt
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
startsWith
1766.0 Ops/sec
.charAt
6905.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare the performance of two approaches: `endsWith` and `.charAt`. The test is focused on measuring which approach is faster when checking if a string ends with a specific character, in this case, '9'. **Script Preparation Code** The provided script preparation code generates an array of random strings (`data`) using a combination of uppercase letters, lowercase letters, digits, and special characters. This is done to create a large dataset for the benchmark. **Html Preparation Code** The HTML preparation code consists of a simple `<div>` element, which is not directly related to the performance test being run. **Individual Test Cases** There are two individual test cases: 1. **".endsWith"**: This test case checks if each string in the `data` array ends with '9' using the `endsWith` method. 2. **".charAt"`: This test case checks if each string in the `data` array has the character at its last index (i.e., the character that would be accessed by indexing the string with `TOTAL_STRINGS - 1`) equal to '9' using the `.charAt()` method. **Comparison of Approaches** The two approaches being compared are: * **.endsWith**: This method checks if a string ends with a specific substring (in this case, '9'). It is a relatively fast and efficient way to perform this type of check. * **.charAt**: This method accesses the last character of a string by indexing it with `TOTAL_STRINGS - 1`. While this approach can be fast for accessing individual characters in a string, it may not be as efficient as using `endsWith` when checking if a string ends with a specific substring. **Pros and Cons** * **.endsWith**: + Pros: Efficient for checking if a string ends with a specific substring. + Cons: May have additional overhead due to the use of a loop or recursive function calls (not explicitly shown in the benchmark code). * **.charAt**: + Pros: Can be fast for accessing individual characters in a string. + Cons: May not be as efficient for checking if a string ends with a specific substring, as it involves indexing into the string. **Library and Special JS Features** There is no explicit mention of any libraries or special JavaScript features being used in this benchmark. The code appears to only use built-in JavaScript methods and functionality. **Other Alternatives** If you're looking for alternative approaches to checking if a string ends with a specific substring, some options might include: * Using regular expressions (e.g., ` RegExp.test()`) * Utilizing a library like Lodash's `endsWith()` function * Implementing a custom loop-based approach using the `indexOf()` method Keep in mind that the choice of approach will depend on the specific requirements and constraints of your use case.
Related benchmarks:
.startsWith vs .charAt for single character
.endsWith vs .charAt for single character
.startsWith vs .charAt vs str[0] for single character
.startsWith vs .charAt vs .charCodeAt for single character
Comments
Confirm delete:
Do you really want to delete benchmark?