My array is quite large, resembling the following:
var counts = ["gfdg 34243", "jhfj 543554", ....] // With a length of 55268 elements
This is my current loop:
var replace = "";
var scored = 0;
var qgram = "";
var score1 = 0;
var len = counts.length;
function score(pplaintext1) {
qgram = pplaintext1;
for (var x = 0; x < qgram.length; x++) {
for (var a = 0, len = counts.length; a < len; a++) {
if (qgram.substring(x, x + 4) === counts[a].substring(0, 4)) {
replace = parseInt(counts[a].replace(/[^1-9]/g, ""));
scored += Math.log(replace / len) * Math.LOG10E;
} else {
scored += Math.log(1 / len) * Math.LOG10E;
}
}
}
score1 = scored;
scored = 0;
} // This function needs to be called roughly 1000 times
I need to iterate through this array multiple times and my code is slowing down. I'm wondering what would be the most efficient way to iterate through this array to maximize time savings.