Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
1df693fa-fdb3-4a94-9030-2e979bfdcb09
(version: 0)
Comparing performance of:
one vs two vs three
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function one(val) { return val.endsWith(".ts") || val.endsWith(".tsx") } function two(val) { const x1 = val.slice(-3); if(x1 === ".ts") return true; const x2 = val.slice(-4); if(x2 === ".tsx") return true; return false; } function three(val) { const L = val.length; if(val[L-3] === '.' && val[L-2] === 't' && val[L-1] === 's') return true; if(val[L-4] === '.' && val[L-3] === 't' && val[L-2] === 's' && val[L-1] === "x") return true; return false; }
Tests:
one
one("hello world.tsx")
two
two("hello world.tsx")
three
three("hello world.tsx")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
one
two
three
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring JavaScript performance is crucial, especially when it comes to web applications that often rely on dynamic imports and file system interactions. The provided JSON represents a benchmarking test for three different functions: `one`, `two`, and `three`. Each function is designed to detect whether a given input string ends with ".ts" or ".tsx". **Function 1: `one`** This function uses the `endsWith()` method, which checks if a string ends with a specified value. ```javascript function one(val) { return val.endsWith(".ts") || val.endsWith(".tsx"); } ``` Pros: * Simple and straightforward implementation. * Efficient, as it only requires a single call to `endsWith()`, regardless of the input length. Cons: * May be slower for very large inputs due to the string slicing operation required by `endsWith()`. **Function 2: `two`** This function uses string slicing to extract the last three and four characters from the input string, respectively. ```javascript function two(val) { const x1 = val.slice(-3); if (x1 === ".ts") return true; const x2 = val.slice(-4); if (x2 === ".tsx") return true; return false; } ``` Pros: * This implementation avoids the overhead of `endsWith()` and can be more efficient for very large inputs. * It's a good example of how to use string slicing effectively in JavaScript. Cons: * Requires two separate calls to `slice()`, which might be slower than calling `endsWith()` once. * The variable names could be improved for better readability. **Function 3: `three`** This function uses the length property and character indexing to check if the last three characters are ".ts" or the last four characters are ".tsx". ```javascript function three(val) { const L = val.length; if (val[L-3] === '.' && val[L-2] === 't' && val[L-1] === 's') return true; if (val[L-4] === '.' && val[L-3] === 't' && val[L-2] === 's' && val[L-1] === "x") return true; return false; } ``` Pros: * This implementation is very efficient, as it only requires a single pass through the input string. * It's an example of how to use length properties and character indexing effectively. Cons: * The variable names could be improved for better readability. **Library: `slice()`** The `slice()` method is a built-in JavaScript function that returns a new string containing a subset of characters from the original string. In this benchmark, `slice()` is used to extract specific parts of the input string. Pros: * Very efficient and widely supported. * Part of the standard JavaScript library. Cons: None notable. **Special JS feature or syntax:** None mentioned in this benchmark. Now, let's talk about alternatives: 1. **Using a regular expression**: You could use a regular expression to achieve the same result as `slice()` or `endsWith()`. For example: ```javascript function two(val) { const regex = /\.ts$/i; return regex.test(val); } ``` This approach is more concise but might be slower for very large inputs. 2. **Using a dedicated library**: If you need to perform complex string matching or pattern recognition, consider using a dedicated library like `regex-js` or `string-match`. 3. **Native string methods**: For some platforms, there might be native string methods that offer better performance than built-in JavaScript methods. For example, in Node.js, you can use the `path.extname()` method to check file extensions. When choosing an implementation, consider factors like performance, readability, and maintainability.
Related benchmarks:
Switch vs map with string keys and early exit
Switch vs map with string keys and early exit 2
Switch vs Object Literal w/out console.log 3
Switch vs Object Literal w/out console.log 4
Multiple Nil checks 0.6
Comments
Confirm delete:
Do you really want to delete benchmark?