Building Strings from Arrays

Benchmark created on


Description

This compares various methods of creating a string out of an array of values to determine which is faster.

Setup

var testArray = [
  "quickfish", "lazytree", "brightrun", "darkbook", "smalljump",
  "largecat", "fastswim", "slowpen", "hotcup", "coldbox",
  "newkey", "oldball", "highbird", "lowrock", "bigleaf",
  "tinystar", "loudmoon", "quietsun", "smoothrain", "roughwind",
  "catfly", "dogwalk", "cartalk", "treesing", "bookdance",
  "penread", "cupwrite", "boxplay", "keywork", "ballsleep",
  "fisheat", "birddrink", "rocklaugh", "leafcry", "starthink",
  "moonlearn", "sunteach", "rainrun", "windjump", "firefly",
  "quickswim", "lazywalk", "brighttalk", "darksing", "smalldance",
  "largeread", "fastwrite", "slowplay", "hotwork", "coldsleep",
  "neweat", "olddrink", "highlaugh", "lowcry", "bigthink",
  "tinylearn", "loudteach", "quietrun", "smoothjump", "roughfly",
  "fishquick", "birdlazy", "rockbright", "leafdark", "starsmall",
  "moonlarge", "sunfast", "rainslow", "windhot", "firecold",
  "catnew", "dogold", "carhigh", "treelow", "bookbig",
  "pentiny", "cuploud", "boxquiet", "keysmooth", "ballrough",
  "swimcat", "walkdog", "talkcar", "singtree", "dancebook",
  "readpen", "writecup", "playbox", "workkey", "sleepball",
  "eatfish", "drinkbird", "laughrock", "cryleaf", "thinkstar",
  "learnmoon", "teachsun", "runrain", "jumpwind", "flyfire",
  "hotquick", "coldlazy", "newbright", "olddark", "highsmall"
];

Test runner

Ready to run.

Testing in
TestOps/sec
If inside loop
let result = '';
for (let i = 0; i < testArray.length; ++i) {
	if (result.length) {
		result += ' ';
	}
	result += testArray[i];
}
ready
Substring after concatenating
let result = '';
for (let i = 0; i < testArray.length; ++i) {
	result += ' ' + testArray[i];
}
result = result.substring(1);
ready
Conditional statement
let result = '';
for (let i = 0; i < testArray.length; ++i) {
	result && (result += ' ');
	result += testArray[i];
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.