Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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.

like image 819
zwol Avatar asked Sep 17 '10 01:09

zwol


1 Answers

For ES6:

'x'.repeat(10*1024*1024) 
like image 195
alex_1948511 Avatar answered Oct 11 '22 05:10

alex_1948511