Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lastIndexOf vs split vs regex
(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[0] const r2 = splited[splited.length - 1]
regex
const splited = str.match(/[^\/]+/g) const r1 = splited[0] const r2 = splited[splited.length - 1]
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:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0
Browser/OS:
Firefox 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lastIndexOf
2414932224.0 Ops/sec
split
14326296.0 Ops/sec
regex
6945606.5 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, compared, and its pros and cons. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmarking test case on MeasureThat.net. The test is designed to compare the performance of three different approaches: `lastIndexOf`, `split`, and regular expressions (`regex`) for extracting a specific pattern from a string. **Approaches Compared** 1. **`lastIndexOf`**: This method searches for the last occurrence of a specified value in a string and returns its index. 2. **`split`**: This method splits a string into an array of substrings based on a specified separator (in this case, the `/` character). 3. **`regex`** (regular expressions): This approach uses a pattern to match a specific sequence of characters in a string and extracts the matched substring. **Pros and Cons** 1. **`lastIndexOf`**: * Pros: Simple, straightforward implementation, fast execution. * Cons: May not be suitable for all use cases (e.g., finding the last occurrence of a value that may not exist). 2. **`split`**: * Pros: Easy to implement, works well for simple string splitting tasks. * Cons: May not be efficient for large strings or complex splitting requirements. 3. **`regex`**: * Pros: Flexible, can match complex patterns, and is suitable for most use cases. * Cons: May have higher overhead due to the need to compile and execute a regular expression pattern. **Library Usage** None of the test cases explicitly use any JavaScript libraries besides the built-in `String` methods. However, if we were to consider the broader context of these methods, we could argue that `lastIndexOf`, `split`, and regular expressions are part of the standard JavaScript API. **Special JS Feature or Syntax** There is no specific special feature or syntax used in this benchmarking test case beyond what's required for each approach. The code snippets provided demonstrate basic string manipulation techniques. **Other Alternatives** If you're looking for alternative approaches to extract a pattern from a string, some other options include: * Using `String.prototype.replace()` with a callback function. * Utilizing `String.prototype.indexOf()` and manual handling of the match result. * Leveraging a library like Lodash or Ramda for more complex string manipulation tasks. In conclusion, this benchmarking test case provides a good representation of the performance differences between three common approaches to extracting patterns from strings in JavaScript.
Related benchmarks:
index vs lastindexof startsWith
index vs lastindexof empty
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?