简体   繁体   English

通过嵌套对象 JavaScript 为递归迭代计时

[英]Timing a recursive iteration through a nested object JavaScript

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.嵌套对象的格式表现出独特的行为 - 每个嵌套对象要么具有1.一个名为"content"的属性,其值为一维元素数组,要么具有2.不同级别的嵌套对象,其属性最终缩小到 (1 ) 以上。

I have a recursive function to search the content arrays of the entire obj as follows:我有一个递归函数来搜索整个 obj 的content数组,如下所示:

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.实际的 obj 是一个深度嵌套的对象,具有相当多的数据条目。 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.我想运行基准测试以获得在任何嵌套对象的"contents"数组中搜索元素所需的平均时间。 My question is - How do I recognize last for...in loop of the parent object obj while recursively looping through it?我的问题是 - 我如何在递归循环时识别父对象obj最后一个for...in循环? (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.我尝试将for loop完成循环遍历"contents"数组(对于每个嵌套对象)的时间推送到全局execTime数组,然后在搜索函数之外使用setTimeout函数来获取maxminexecTime因为我不知道什么时候递归搜索功能停止执行。

@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) @VLAZ 的评论证明非常有用:我已经决定使用以下内容来运行基准测试(仍然没有找到 *in-code 解决方案,但这完成了工作)

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.您可以让您的搜索函数返回总执行时间,您可以将其添加到初始search调用中。

When the search function hits the return , that will be your last recursive call.当搜索函数命中return ,这将是您最后一次递归调用。 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.例如,在最后一次调用时, cLength将为零,这意味着不需要进一步搜索, else将在之后执行点击return这将一直发生到初始search调用。

You could use the User Timing API, in the browser您可以在浏览器中使用 User Timing API

 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM