So I have a loop that works great, but doing some testing I notice it is a bit slow. I get an average time within the loop of about 0.11 seconds. Include that with some arrays containing a length of over 100 and the time starts making a big difference. So I mainly want to improve the speed of the loop without compromising the functionality it already has (which is taking a JSON object/array and turning it into an html format). Below is the code along with the function that is called within the loop.
let replacements = '';
let ind = 0;
for (let w in obj[key]) {
if (Array.isArray(obj[key])) {
if (Array.isArray(obj[key][w])) {
if (obj[key].length > 10) {
replacements += "<font color='green'>Array(" + obj[key].length + ")</font>"
break
}
replacements += ((ind == 0) ? "<font color='green'>Array(" : ", <font color='green'>Array(") + obj[key][w].length + ")</font>";
} else {
if (typeof obj[key][w] === 'object') {
replacements += ((ind == 0) ? "" : ", ") + "{...}";
} else {
replacements += ((ind == 0) ? "" : ", ") + ObjectString(obj[key][w], "preview");
}
}
} else {
if (Array.isArray(obj[key][w]) || typeof obj[key][w] === 'object') {
replacements += ((ind == 0) ? "" : ", ") + w + ": " + ((Array.isArray(obj[key][w])) ? "[...]" : "{...}");
} else {
replacements += ((ind == 0) ? "" : ", ") + w + ": ";
replacements += ObjectString(obj[key][w], "preview");
}
}
ind++;
}
function ObjectString(obj, type) { //~0.001-0.003 seconds
let objString = obj;
if (typeof objString == "string" && !objString.match(/<.*?>/g))
objString = "<font color='red'>\"" + objString + "\"</font>";
else if (/<.*?>/g.test(objString))
objString = ((type == "normal") ? "<pre>" + process(objString).replace(/</g, '<').replace(/>/g, '>') + "</pre>" + objString : "<font color='red'>\"...\"</font>");
else if (typeof objString == "number" || typeof objString == "boolean")
objString = "<font color=' #947cf6'>" + objString + "</font>";
return objString
}
function process(str) {//~0.001 seconds
var div = document.createElement('div');
div.innerHTML = str.trim();
return format(div, 0).innerHTML;
}
Hopefully this can be optimized to increase its speed, but if it can't then I would at least like some help to clean it up as I am sure this isn't the most effective way to do it. Thanks!
Edit: added the process function that was previous missing. Also for clarification I am using regular JSON arrays and objects that usually exceed 100 objects in the array. If you want an example of what that data could look like https://en.wikipedia.org/wiki/GeoJSON#Example has a good structure of some of the data I am parsing (containing both objects and arrays).
for... in
I think is slower than other methods, like a straightfor(let x = 0; x < length; x++)
method. To clean this up, you may want to look at a simple template engine like mustache. That would remove your html and probably do most of the heavy lifting. \$\endgroup\$for...in
statement, they are generally slower. I tested moving it to a standard loop, but it seems like the major break is the check if its an array or object. \$\endgroup\$JSON.parse(JSON.strigify(obj))
as a cast method? \$\endgroup\$Object.keys(obj).map()
instead of checks. \$\endgroup\$isArray
andtypeof
can execute millions of times in 110ms and not the cause of the slowdown. It is the DOM as you create a div and then add HTML to it inprocess
. That will be very slow, also the functionformat
what ever it does? could make it even worse \$\endgroup\$