Most efficient way to generate a really long string (tens of megabytes) in JS

Viewed 21941

I find myself needing to synthesize a ridiculously long string (like, tens of megabytes long) in JavaScript. (This is to slow down a CSS selector-matching operation to the point where it takes a measurable amount of time.)

The best way I've found to do this is

var really_long_string = (new Array(10*1024*1024)).join("x");

but I'm wondering if there's a more efficient way - one that doesn't involve creating a tens-of-megabytes array first.

5 Answers

For ES6:

'x'.repeat(10*1024*1024)
Related