Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Get words ending in number
(version: 0)
Comparing performance of:
regex vs 1 vs 2 vs 3
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function encontrarTermosComNumeroFinal1(input) { const palavras = input.split(' '); const termosComNumeroFinal = palavras.filter(palavra => { const ultimoCaractere = palavra[palavra.length - 1]; return !isNaN(parseInt(ultimoCaractere)); }); return termosComNumeroFinal; } function encontrarTermosComNumeroFinal2(input) { return input.split(' ').filter(palavra => { const ultimoCaractere = palavra.charCodeAt(palavra.length - 1); return ultimoCaractere >= 48 && ultimoCaractere <= 57; }); } function encontrarTermosComNumeroFinal3(input) { return input.split(' ').filter(palavra => palavra[palavra.length - 1] >= '0' && palavra[palavra.length - 1] <= '9'); }
Tests:
regex
'Este é um teste1 com vários termos2 e números3'.match(/[a-z]+\d/g);
1
encontrarTermosComNumeroFinal1('Este é um teste1 com vários termos2 e números3')
2
encontrarTermosComNumeroFinal2('Este é um teste1 com vários termos2 e números3')
3
encontrarTermosComNumeroFinal3('Este é um teste1 com vários termos2 e números3')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
regex
1
2
3
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
regex
4813274.0 Ops/sec
1
526127.1 Ops/sec
2
8632408.0 Ops/sec
3
8414471.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
To analyze the provided benchmark, I'll break down what's being tested, compare different approaches, and discuss their pros and cons. **Benchmark Description** The benchmark measures how efficiently JavaScript can extract words ending in numbers from a given input string. There are three test cases: 1. **Regex (Test Case 2)**: Uses regular expressions to match words with digits at the end. 2. **`encontrarTermosComNumeroFinal1` function**: A custom JavaScript function that splits the input into individual words, filters out those ending in a number using `parseInt`, and returns the remaining words. 3. **`encontrarTermosComNumeroFinal2` function**: Another custom JavaScript function that uses `charCodeAt` to check if the last character of each word is a digit (48-57). 4. **`encontrarTermosComNumeroFinal3` function**: A third custom JavaScript function that checks if the last character of each word is between '0' and '9'. **Comparison of Approaches** * **Regex (Test Case 2)**: Pros: + Efficient, as regular expressions can be optimized for performance. + Portable across different browsers and platforms. * Cons: + May have security implications if not properly sanitized. + Can be slow due to the complexity of regular expression matching. * `encontrarTermosComNumeroFinal1` function: Pros: + Easy to understand and maintain. + No external dependencies or libraries required. * Cons: + Can be slower than regex-based approaches, as it involves iterating over each word character by character. + May not perform well for large input strings. * `encontrarTermosComNumeroFinal2` function: Pros: + Simple and easy to understand. + No external dependencies or libraries required. * Cons: + Less efficient than regex-based approaches, as it requires explicit iteration over each character. + May not perform well for large input strings. * `encontrarTermosComNumeroFinal3` function: Pros: + Similar to the previous one, with a simple and easy-to-understand approach. * Cons: + Same drawbacks as the previous one. **Library Usage** None of the test cases use external libraries. **Special JS Features or Syntax** There are no specific JavaScript features or syntax being used in these benchmarks. However, the `charCodeAt` method is used in Test Case 2, which can be considered a "legacy" feature from older versions of JavaScript. If modern JavaScript environments were to be tested instead, this might not be necessary. **Other Alternatives** Alternative approaches for extracting words ending in numbers could include: 1. **Using a dedicated natural language processing (NLP) library**: Such as `lodash` or `moment`, which provide more advanced features and optimizations for text analysis. 2. **Regular expression alternatives**: Like `\b\w*\d+\b` or `\b\w*(?=\d+)\b`, which might be more efficient than the standard regex used in Test Case 2. 3. **Using a JavaScript implementation of the Boyer-Moore string searching algorithm**: Which could provide improved performance for large input strings. Keep in mind that these alternatives may add complexity to the benchmark and require additional setup or dependencies.
Related benchmarks:
switch vs arrow functions returned JSON
Map Vs Switch
if-else vs switch v2
switch(true) vs if-else
IndexOf vs Includes in string - larger string edition
Comments
Confirm delete:
Do you really want to delete benchmark?