0

Below is my code which is taking time in the for loop to extract data from one object and filling another object. Is there any way to reduce the time of execution? I have tried a while loop but it is not helping that much. 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.

5
  • 3
    You could try slice to take a subsection of your array in one go, rather than pushing into a new array, which may involve reallocation and expansion of that array as it grows. But I doubt even that would be slow for 100 elements. Commented Jun 10, 2016 at 10:07
  • 1
    Also since you're using i < limiter not i <= limiter you probably want to add the 100 again, not 99. Commented Jun 10, 2016 at 10:08
  • sorry the last comment i haven't get can you explain me a bit more is there any mistake i mean it will fetch next 100 always right? Commented Jun 10, 2016 at 10:11
  • thanks it wont take 100th data i got it thanks a lot Commented Jun 10, 2016 at 10:33
  • 1
    Go with slice, as @Rup pointed out. As a note: Preallocating arrays via new Array(length) is always faster than dynamic expanding them in a loop with known bounds. Commented Jun 10, 2016 at 15:20

1 Answer 1

1

You could try caching result.length at the beginning of your function (following the if check at the beginning)..

   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]);
                }

            }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.