简体   繁体   English

如何减少执行JavaScript代码的时间

[英]How to reduce time of execution for javascript code

Below is my code which is taking time in the for loop to extract data from one object and filling another object. 下面是我的代码,它在for循环中花时间从一个对象中提取数据并填充另一个对象。 Is there any way to reduce the time of execution? 有什么方法可以减少执行时间? I have tried a while loop but it is not helping that much. 我尝试了一个while循环,但它没有太大帮助。 Kindly help 请帮助

function SetGridWithData(result) {
    if (!result) {
        return;
    }
    CtrlBillableItem_SearhedBillableItems = result
    var boxOfJson = [];
    var j = 100;
    if (result.length >= 100) {
        if (PagingLastRecNum == 0) {

            btnPrevious.style.display = 'none';
            for (var i = 0; i < j; i++) {
                boxOfJson.push(result[i]);
            }
        } else {
            btnPrevious.style.display = 'inline';
            var intializer = (j * PagingLastRecNum) + PagingLastRecNum;
            var limiter = intializer + 99;
            for (var i = intializer; i < limiter; i++) {
                boxOfJson.push(result[i]);
            }
        }

    } else {
        btnPrevious.style.display = 'none';
        btnNext.style.display = 'none';

        for (var i = 0; i < result.length; i++) {
            boxOfJson.push(result[i]);
        }

    }
}

I am trying to implement paging which is done, but 100 data per page first it will check page no 0 if it is then loop one and if other than 0 than else case. 我正在尝试实现已完成的分页,但是每页首先有100个数据,如果它随后循环一个,并且除其他情况外不是0,它将检查第0页。

You could try caching result.length at the beginning of your function (following the if check at the beginning).. 您可以尝试在函数的开头缓存result.length (在开头的if检查之后)。

   function SetGridWithData(result) {
            if (!result) { return; }
            var resultLength = result.length;
            CtrlBillableItem_SearhedBillableItems = result
            var boxOfJson = [];
            var j = 100;
            if (resultLength >= 100) {
                if (PagingLastRecNum == 0) {

                    btnPrevious.style.display = 'none';
                    for (var i = 0; i < j; i++) {
                        boxOfJson.push(result[i]);
                    }
                }
                else {
                    btnPrevious.style.display = 'inline';
                    var intializer = (j * PagingLastRecNum) + PagingLastRecNum;
                    var limiter = intializer + 99;
                    for (var i = intializer; i < limiter; i++) {
                        boxOfJson.push(result[i]);
                    }
                }

            }
            else {
                btnPrevious.style.display = 'none';
                btnNext.style.display = 'none';

                for (var i = 0; i < resultLength; i++) {
                    boxOfJson.push(result[i]);
                }

            }
}

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

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