Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Closing tags
(version: 0)
Banana
Comparing performance of:
Loop with moving IndexOf vs While loop with substring
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var rawText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, <!--sed do </style>eiusmod tempor incididunt u-->t labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud </style> exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'
Tests:
Loop with moving IndexOf
function escapeMatchingClosingTag(rawText, parentTag) { const text = [...rawText]; const lowercasedRawText = rawText.toLowerCase(); const pattern = '</' + parentTag.toLowerCase() + '>'; let start = 0; while(start < lowercasedRawText.length) { const indexOfClosingTag = lowercasedRawText.indexOf(pattern, start); if (indexOfClosingTag === -1) { break; } const indexOfCommentStart = lowercasedRawText.indexOf('<!--', start); if (indexOfCommentStart === -1) { // No Comment found text[indexOfClosingTag] = '<'; start =indexOfClosingTag + pattern.length-1; } else if (indexOfCommentStart < indexOfClosingTag) { const indexOfCommentEnd = lowercasedRawText.indexOf('-->', indexOfCommentStart+4); if (indexOfClosingTag > indexOfCommentEnd) { // Closing tag is not comment. text[indexOfClosingTag] = '<'; start =(indexOfClosingTag + pattern.length-1); } else { start =(indexOfCommentEnd + 2); } } } return text.join('') } escapeMatchingClosingTag(rawText, 'style');
While loop with substring
function escapeMatchingClosingTag(rawText, parentTag) { let result = ''; let i = 0; let isInCommentBlock = false; const lowercasedRawText = rawText.toLowerCase(); const pattern = '</' + parentTag.toLowerCase() + '>'; while (i < rawText.length) { let currentChar = rawText[i]; if (isInCommentBlock) { if (rawText.substr(i, 3) === '-->') { // We came across a closing comment tag while being in // a comment block. Set the flag and proceed to the next char. isInCommentBlock = false; } } else { if (rawText.substr(i, 4) === '<!--') { // We came across an open comment tag, // set the flag and proceed to the next char. isInCommentBlock = true; } else { // We are not in comment block, check if we have // a lookup pattern in front of us. Compare against // a lowercased content to avoid case sensitivity. if (lowercasedRawText.substr(i, pattern.length) === pattern) { currentChar = '<'; // replace '<' with '<' } } } result += currentChar; i++; } return result; } escapeMatchingClosingTag(rawText, 'style');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Loop with moving IndexOf
While loop with substring
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):
**What is being tested?** MeasureThat.net is testing the performance of JavaScript functions that replace closing tags in a given text with their HTML-encoded equivalents (`<` instead of `>`). The tests compare two different approaches to achieve this: 1. **Loop with moving IndexOf**: This approach uses a while loop and the `indexOf()` method to find the position of the closing tag pattern in the lowercased text. 2. **While loop with substring**: This approach uses a traditional while loop and string slicing (substring) to compare the current character with the closing tag pattern. **Options compared** The two approaches are being tested against each other, allowing users to compare their performance and choose the best one for their use case. **Pros and Cons of each approach:** 1. **Loop with moving IndexOf** * Pros: + Efficient use of `indexOf()` method, which is optimized by JavaScript engines. + Less memory allocation and copying of strings compared to string slicing. * Cons: + May have issues with performance on older browsers or those with limited resources. + Can be less intuitive for developers unfamiliar with the `indexOf()` method. 2. **While loop with substring** * Pros: + More straightforward and easier to understand, especially for developers without prior experience with `indexOf()`. + May perform better on older browsers or those with limited resources. * Cons: + Less efficient use of string slicing (substring), which can be slower than `indexOf()`. **Library used** None is explicitly mentioned in the provided benchmark definitions. However, it's worth noting that some JavaScript engines might have built-in optimizations or caching mechanisms for certain functions, like `indexOf()`, that could affect performance. **Special JS features or syntax** None are explicitly mentioned in the provided benchmark definitions. However, it's always important to consider the impact of specific JavaScript features, such as async/await, closures, or object destructuring, on performance when writing benchmarks. **Other alternatives** If the test cases were modified to include other approaches, some potential alternatives could be: 1. **Regular expressions**: Using regular expressions to replace closing tags with HTML-encoded equivalents. 2. **String replacement methods**: Using built-in string replacement methods like `replace()` or `replaceAll()`. 3. **Template literals**: Using template literals with a custom string literal tag (`'tag name'`) to achieve the desired result. These alternatives would require significant modifications to the benchmark definitions and might not provide direct comparisons to the original approaches.
Related benchmarks:
IndexOf vs Includes in Larger string
Test clientWidth, width, offsetWidth
class vs data-attribute
Javascript: Case insensitive string comparison performance 3
Comments
Confirm delete:
Do you really want to delete benchmark?