I have an object with the following structure.
var obj = {
"a":{"content": [/*elements*/]},
"b":{
"d":{
"g":{"content":[/*elements*/]},
"h":{
"j":{"content":[/*elements*/]},
"k":{"content":[/*elements*/]}
},
"i":{
"l":{"content":[/*elements*/]}
}
},
"e":{"content":[/*elements*/]},
"f":{"content":[/*elements*/]}
},
"c":{"content":[/*elements*/]},
/* Object goes on with different levels of nesting*/
};
The format of the nested object exhibits a unique behavior - Each nested object either has 1. one property named "content"
whose value is a 1-D array of elements, or 2. Different levels nested objects whose properties finally narrow down to (1) above.
I have a recursive function to search the content
arrays of the entire obj as follows:
function search(index) {
for(var key in index) {
var current = index[key];
var cLength = Object.keys(current).length;
if(cLength > 1 ) {
search(current);
} else {
if (index[key]["content"] == undefined) {
search(current);
} else {
contentsArray = index[key]["content"];
// Search Contents Array
}
}
}
}
search(obj);
The actual obj is a deeply nested object with quite a lot of data entries. I want to run benchmark tests to get the average time it would take to search for an element in the "contents"
array any of the nested objects. My question is - How do I recognize last for...in
loop of the parent object obj
while recursively looping through it? (To record the finish time) Is there a better way to time the execution of such a function?
I've tried pushing the times at which a for loop
finishes looping through the "contents"
array (for each nested object) to a global execTime
array, then using a setTimeout
function outside of the search function to get the difference between the max
and min
values in execTime
since I'm not sure when the recursive search function stops executing.
@VLAZ's comment proved quite useful: I've settled to using the following to run the benchmark tests (Still haven't figured out a *in-code solution, but this gets the job done)
console.time("recursion");
search(obj);
console.timeEnd("recursion");
Heres a working demo
You can have your search function return the total execution time that you can add back up all the way to the initial search
call.
When the search function hits the return
, that will be your last recursive call. For example, on the last call cLength
will be zero, meaning further search is not needed and the else
will execute hitting the return
after.This will keep happening all the way to the initial search
call.
You could use the User Timing API, in the browser
function doStuff() { // long running } performance.mark('start-token'); doStuff(); performance.mark('end-token'); performance.measure('time', 'start-token', 'end-token'); console.log('time: ', performance.getEntriesByType("measure")[0].duration); performance.clearMarks(); performance.clearMeasures();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.