Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Split into whole words
(version: 0)
Comparing performance of:
my answer vs my answer non-destructive
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var input = 'There was no possibility of taking a walk that day. We had been wandering, indeed, in the leafless shrubbery an hour in the morning; but since dinner (Mrs. Reed, when there was no company, dined early) the cold winter wind had brought with it clouds so sombre, and a rain so';
Tests:
my answer
function splitText(text, maxLength = 30) { const result = []; while (text !== '') { let endIndex; if (maxLength > text.length) { // [A] endIndex = text.length; } else if (text.substring(maxLength, 1) !== ' ') { // [B} endIndex = text.lastIndexOf(' ', maxLength); } else { endIndex = maxLength; } result.push(text.substring(0, endIndex)); text = text.substr(endIndex).trim(); } return result; } console.log(splitText(input));
my answer non-destructive
function splitText(text, maxLength = 30) { const result = [], ubound = text.length; let currentPos = 0; debugger; while (currentPos < ubound) { let endIndex = currentPos + maxLength; if (endIndex > ubound) { // [A] endIndex = text.length; } else if (text.substring(endIndex, 1) !== ' ') { // [B} endIndex = text.lastIndexOf(' ', endIndex) + 1; } result.push(text.substring(currentPos, endIndex).trim()); currentPos = endIndex; } return result; } console.log(splitText(input));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
my answer
my answer non-destructive
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 dive into the provided benchmark definition and test cases. **Benchmark Definition:** The benchmark is designed to measure the performance of JavaScript code that splits a given text string into an array of words, with a maximum length limit (defaulting to 30 characters). **Script Preparation Code:** The script preparation code provides the input text for the benchmark: ```javascript var input = 'There was no possibility of taking a walk that day. We had been wandering, indeed, in the leafless shrubbery an hour in the morning; but since dinner (Mrs. Reed, when there was no company, dined early) the cold winter wind had brought with it clouds so sombre, and a rain so'; ``` This input text is a long sentence that will be split into words. **Html Preparation Code:** There is no HTML preparation code provided, which means that the benchmark does not require any additional HTML setup or parsing. **Test Cases:** There are two test cases: 1. **"my answer"**: This test case uses the traditional approach to splitting the text string into words. The `splitText` function iterates through the input text and pushes each word into the `result` array using the `substring` method. 2. **"my answer non-destructive"**: This test case uses a slightly different approach, where the `splitText` function also modifies the original input string by assigning it to the `text` variable. **Libraries:** None of the test cases use any external libraries or frameworks. **Special JavaScript Features/Syntax:** * The `trim()` method is used in both test cases to remove whitespace from the ends of each word. * The `substring()` method is used to extract substrings from the input text. * The `while` loop is used to iterate through the input text and push words into the `result` array. **Pros and Cons of Approaches:** 1. **Traditional Approach ( "my answer" )**: * Pros: + Simple and straightforward implementation. + Easy to understand and maintain. * Cons: + May be slower due to unnecessary string manipulations. + Can lead to performance issues with large input strings. 2. **Non-Destructive Approach ( "my answer non-destructive" )**: * Pros: + Preserves the original input string, which can reduce memory usage. + May be faster due to reduced string manipulation. * Cons: + More complex implementation. + May require additional memory allocations. **Other Considerations:** * The benchmark measures the performance of the JavaScript code in terms of executions per second (ExecutionsPerSecond) on a specific device and browser configuration. * The `RawUAString` field provides information about the user agent string, which can be used to identify the browser and device being used. **Alternatives:** Some alternative approaches for splitting text strings into words include: * Using the `split()` method, which is supported by most modern browsers. * Using a library like `lodash` or `underscore`, which provide optimized string manipulation functions. * Using a more efficient algorithm, such as a sliding window approach. However, these alternatives are not represented in the provided benchmark test cases.
Related benchmarks:
replacing node text
asdvcxvxvxvxvxvxvxvxv
testing my code
CloningBenchmark01
Multiple substrings
Comments
Confirm delete:
Do you really want to delete benchmark?