Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lastIndexOf vs split vs regex v2
(version: 0)
testing speed
Comparing performance of:
lastIndexOf vs split vs regex
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = 'player/attack-bottom/1'
Tests:
lastIndexOf
const idx = str.lastIndexOf('/') const r1 = str.substring(0, idx) const r2 = str.substring(idx + 1, str.length)
split
const splited = str.split('/') const r1 = splited.slice(-1).join('/') const r2 = splited[splited.length - 1]
regex
const [_, r1, r2] = str.match(/(.+)\/(\d+)$/)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lastIndexOf
split
regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lastIndexOf
8151699.5 Ops/sec
split
12760170.0 Ops/sec
regex
13865260.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks to compare the performance of different approaches on various platforms. The provided benchmark definition is for three test cases: 1. `lastIndexOf` 2. `split` 3. `regex` These test cases aim to measure the speed of each approach in extracting the last occurrence of a character or substring from a string. **Options Compared** Each test case compares different approaches to achieve the same result: * `lastIndexOf`: Uses the built-in `String.prototype.lastIndexOf()` method to find the last index of a character or substring. * `split`: Splits the input string into an array of substrings using the `/` character as the delimiter, then extracts the last element from the array. * `regex`: Uses regular expressions (regex) to extract the last occurrence of a pattern from the input string. **Pros and Cons of Each Approach** Here's a brief summary of the pros and cons of each approach: 1. **lastIndexOf**: * Pros: Simple, efficient, and widely supported by most browsers. * Cons: May not work correctly for certain edge cases (e.g., empty strings, null inputs). 2. **split**: * Pros: Can handle complex splitting patterns, but may be slower due to the overhead of creating an array. * Cons: Requires more memory and processing power than `lastIndexOf`, especially for large input strings. 3. **regex**: * Pros: Flexible and powerful, allowing for complex pattern matching and extraction. * Cons: May be slower than `lastIndexOf` and `split` due to the overhead of compiling regex patterns. **Library Usage** In this benchmark, the following libraries are used: 1. None No external libraries are required or used in this benchmark. **Special JS Features/Syntax** None mentioned There is no special JavaScript feature or syntax being tested in this benchmark. **Other Alternatives** If you're looking for alternative approaches to extract the last occurrence of a character or substring from a string, here are some options: 1. **Using `String.prototype.substring()` with a large step value**: This approach can be faster than using `lastIndexOf` but may consume more memory and processing power. 2. **Using a custom function with indexing**: You can write a custom function that uses indexing to extract the last occurrence of a character or substring from a string. **Benchmark Preparation Code** The provided preparation code for each test case is as follows: 1. `lastIndexOf`: ```javascript var str = 'player/attack-bottom/1'; ``` 2. `split`: ```javascript const splited = str.split('/'); const r1 = splited.slice(-1).join('/'); const r2 = splited[splited.length - 1]; ``` 3. `regex`: ```javascript const [_, r1, r2] = str.match(/(.+)\\/(\\d+)$/); ``` **Latest Benchmark Result** The latest benchmark result shows the performance metrics for each test case: | Test Name | Executions Per Second | | --- | --- | | regex | 13865260.0 | | split | 12760170.0 | | lastIndexOf | 8151699.5 | Note that these results are specific to this benchmark and may vary depending on the platform, browser, and other factors.
Related benchmarks:
index vs lastindexof startsWith
index vs lastindexof empty with startIndex set to 0
regex vs split+pop vs lastIndexOf+substring
index vs lastindexof (last index)
Comments
Confirm delete:
Do you really want to delete benchmark?