Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
spanify test
(version: 0)
testing two different methods of spanifying some text
Comparing performance of:
Simple vs Complex
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.simple =([result, input]) => { let regExp = new RegExp(`(${input})`, 'i'); const highlightedResult = result .split(regExp) .map(segment => regExp.test(segment) ? `<mark>${segment}</mark>` : segment) .join('') return highlightedResult; } const separatorCommon = /[\.,\s!;?:"\u3000]+/; function separateTerms(query) { let results = [], words = query .toString() .toLowerCase() .split(separatorCommon); words.forEach(function(word) { if (word && results.indexOf(word) === -1) { results.push(word); } }); results.sort(function(a, b) { return b.length - a.length; }); return results; } window.markSearchTerms = ([value, query]) => { let results = value; if (value && query) { let matchValue = value.toString().toLowerCase(), queryTerms = separateTerms(query), marks = []; queryTerms.forEach(function(term) { let index, start = 0; while ((index = matchValue.indexOf(term, start)) !== -1) { let mark = { a: index, z: index + term.length }, merged = false; for (let i = 0; i < marks.length; i++) { let current = marks[i]; if ( (current.a <= mark.a && mark.a <= current.z) || (current.a <= mark.z && mark.z <= current.z) ) { current.a = Math.min(current.a, mark.a); current.z = Math.max(current.z, mark.z); merged = true; break; } } if (!merged) { marks.push(mark); } start = index + term.length; } }); if (marks.length) { marks.sort(function(a, b) { return a.a - b.a; }); results = []; let index = 0; marks.forEach(function(mark, i) { results.push(value.slice(index, mark.a)); results.push(`<mark>${value.slice(mark.a, mark.z)}</mark>`); index = mark.z; }); results.push(value.slice(index)); } } return results.join(''); }
Tests:
Simple
[ ['test', 'est', 't<mark>est</mark>'], ['test 1', 't 1', 'tes<mark>t 1</mark>'], ['test test', 't t', 'tes<mark>t t</mark>est'], ['test', 'test', '<mark>test</mark>'], ['TEST', 'te', '<mark>TE</mark>ST'], ['test', 'TE', '<mark>te</mark>st'], ['tt', 't', '<mark>t</mark><mark>t</mark>'], ['tete', 'te', '<mark>te</mark><mark>te</mark>'], ['test test hi test', 'test test', '<mark>test test</mark> hi test'], ].forEach(simple)
Complex
[ ['test', 'est', 't<mark>est</mark>'], ['test 1', 't 1', 'tes<mark>t 1</mark>'], ['test test', 't t', 'tes<mark>t t</mark>est'], ['test', 'test', '<mark>test</mark>'], ['TEST', 'te', '<mark>TE</mark>ST'], ['test', 'TE', '<mark>te</mark>st'], ['tt', 't', '<mark>t</mark><mark>t</mark>'], ['tete', 'te', '<mark>te</mark><mark>te</mark>'], ['test test hi test', 'test test', '<mark>test test</mark> hi test'], ].forEach(markSearchTerms)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Simple
Complex
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):
Let's break down the benchmark and its tests. **Benchmark Definition** The benchmark definition is a JSON object that contains information about the test case. It has two main parts: 1. **Script Preparation Code**: This code defines three functions: * `simple`: takes an array of strings as input and returns the highlighted version of the text using regular expressions. * `separatorCommon`: defines a common separator string used to split input text into words. * `separateTerms`: splits the input text into individual terms (words) using the defined separator. 2. **Html Preparation Code**: This field is empty, indicating that no HTML code is required for this benchmark. **Individual Test Cases** The benchmark defines two test cases: 1. **Simple**: This test case uses the `simple` function to highlight words containing certain patterns (e.g., "test", "est", "t<mark>est</mark>"). 2. **Complex**: This test case is similar to the Simple test, but with additional patterns to make it more complex. **Test Logic** The test logic works as follows: 1. The input text is passed through the `separateTerms` function to split it into individual terms (words). 2. The `simple` function is then applied to each term to highlight words containing certain patterns. 3. The resulting highlighted terms are joined together to form the final output. **Library and Special Features** The benchmark uses two libraries: 1. **RegExp**: The `RegExp` library is used in the `simple` function to define regular expressions for highlighting patterns. 2. **String.prototype.split()**: This method is used in the `separateTerms` function to split the input text into individual words. There are no special JavaScript features or syntax used in this benchmark, other than the use of RegExp and the String prototype methods. **Alternatives** If you were to implement this benchmark from scratch, you could consider using alternative approaches: 1. **Use a different highlighting library**: Instead of using the built-in `RegExp` functionality, you could use a dedicated highlighting library like Highlight.js or Prism.js. 2. **Optimize the regular expression engine**: If performance is critical, you might consider optimizing the regular expression engine used in the `simple` function to reduce execution time. 3. **Use a more efficient string splitting algorithm**: Depending on the input text size and complexity, you might consider using a more efficient string splitting algorithm like `Array.prototype.reduce()` or a custom implementation. Overall, this benchmark is designed to test the performance of the browser's JavaScript engine when executing complex highlighting logic using regular expressions and string manipulation techniques.
Related benchmarks:
Array from vs string split
Regex vs split/join - nb fork
double split vs regex
~~(12.34) vs split()
Remove all extra spaces (RegEx vs. Split filter join vs. Loop)
Comments
Confirm delete:
Do you really want to delete benchmark?