-1

I am building a string to be parsed into an array by JavaScript. I can make it delimited or I can make the fields fixed-width.

I have only tested on Windows with Firefox and Chrome, so please run the test from other OSes and browsers. My two test results are clear: String.prototype.split() is the winner by a large margin.

Is my fixed-width code not efficient enough, or is the built-in string split function simply superior? Is there a way to code it so that the fixed-width parsing triumphs? If this was C/C++, the fixed-width code, written properly, would be the clear winner. But I know JavaScript is an entirely different beast.

4
  • I'd suggest putting your code inside a function to better hint the optimiser. But it's still just a microbenchmark. Commented Dec 28, 2019 at 15:21
  • @Bergi - so you're saying the question has no real answer unless I test it within the context of my real-world code in a single run with the largest plausible dataset? The total real-world performance difference might truly be negligible, but I wanted to see. This jsperf uses a small, but real-world dataset, so it seemed like a plausibly useful test. I just put stuff inside functions and the results are essentially the same. Commented Dec 28, 2019 at 15:26
  • 1
    I was thinking of something like this, which also fixes the bug where i < data.length should have been i < array.length - this increases perf by a factor of 16 :-) Still a huge difference though. Commented Dec 28, 2019 at 15:41
  • @Bergi - in Firefox your bug fix makes fixed(data) the winner, so kudos for that. But Firefox is so much slower than Chrome overall on this. Here is another sub-split variation that also shows slice() to be faster than split(). This one is not so much a microbenchmark because I really do loop over the array of strings and split each one: jsperf.com/ss-split-vs-slice Commented Dec 28, 2019 at 17:55

1 Answer 1

2

String.prototype.split() is a built-in JavaScript function. Expect it to be highly optimized for the particular JS engine and be written not in JavaScript but in C++.

It should thus not come a surprise that you can't match its performance with pure JavaScript code.

String operations like splitting a delimited string are inherently memory-bound. Hence, knowing the location of delimiters doesn't really help much, since the entire string still needs to be traversed at least once (to copy the delimited fragments). Fixed-position splitting might be faster for strings that exceed D-cache size, but your string is just 13KB long, so traversing it multiple times isn't going to matter.

Sign up to request clarification or add additional context in comments.

2 Comments

"the entire string still needs to be traversed at least once (to copy the delimited fragments)" - that depends on the string representation, which might only create string objects referencing the same underlying buffer, with different offsets.
Thanks for the answer. I wasn't necessarily surprised, but I wanted to know. If you look at the comments to my original question you'll see some variation in this. In Firefox on Windows the revised fixed-width javascript code runs faster, but way slower than in Chrome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.