You should do a string interpolation or create an Array and then push the values to it.
In the end, you would append the entire string, or use the Array.prototype.join
to turn it in a string.
The for
loop is fast, the problem there is the I/O. Take a look at the example below:
var Combinatorics = require('js-combinatorics');
var fs = require('fs');
var cp = Combinatorics.cartesianProduct(
["4", "@", "/\\", "/-\\", "^", "∂", "λ", "α", "(!", "Z", "α"],
["1²", "2", "?", "P\\", "[\"/_", "l\"/_", "|-", "|2", "|?", "®", "12", "/2", "I2", "|^", "|~", "(r)", "|`", "l2", "Я", "ʁ", "я"],
["#", "(-)", ")-(", "/-/", "4", "<~>", "[-]", "\\-\\", "]-[", "]~[", "{-}", "{=}", "|-|", "|~|", "}{", ":-:", "}-{", "н"],
["!", "'", "1", "[]", "][", "|", "¦", "¡", ":", "]", "ι"],
["&", "3", "€", "£", "ë", "[-", "|=-", "ə", "ε"]
);
fs.appendFile('./output.txt', cp.toArray().join('\n'), function(error) {
if (error)
console.log('Error writing ' + error);
});
Note: another thing I did here was to cache the cp.toArray()
into a variable, so you don’t need to call it once per loop iteration. Doing the both things (write to the file only once, and calling .toArray()
only once either, you’ll have a much better performance, completely sure.
UPDATE
I’ve just realized you don’t need a loop at all, since the only thing you’re doing is concatenating a \n
for each word you have, and you can do that by calling the Array.prototype.join
method.
solved For loop is too slow