Sign up to receive SuiteScript examples and advice directly in your email inbox.

Lookup Time is 8% of Load Time

Created: June 22, 2017

Reader Danny Mashburn wrote in to follow up on the curiosity discussion. His curiosity got the better of him, and he decided to do a little more than a single, anecdotal test run of lookups versus loads.

Here's what Danny had to say (shared with permission):

So your curiosity got my curiosity going. I plugged in your script in once and ran it and thought wow, thats an enormous savings in time! Then I ran it again, and got a slightly different value. Then again and again and I logged 10 results in excel and found an average and then I thought, what if I could run it 10 times automatically, or 20, or 100... So I wrote this:

require(["N/record", "N/search"], function (r, s) {
    var load = [];
    var look = [];
    var ratio = [];

    for (var i = 0; i < 100; i++) {
        var t0 = performance.now()
        var customerId = r.load({
            type: r.Type.SALES_ORDER,
            id: 16536
        }).getValue({fieldId: "entity"});
        var t1 = performance.now();
        var totalLoad = t1 - t0;
        load.push(totalLoad);

        var t2 = performance.now();
        var customerId2 = s.lookupFields({
            type: r.Type.SALES_ORDER,
            id: 16536,
            columns: ["entity"]
        }).entity[0].value;
        var t3 = performance.now();
        var totalLookup = t3 - t2;
        look.push(totalLookup)
        console.log('test3');

        var ratioValue = totalLookup / totalLoad;
        ratio.push(ratioValue)
    }

    var sum = 0
    for (var j = 0; j < ratio.length; j++) {
        sum = sum + ratio[j]
    }
    var average = sum / ratio.length;

    console.log(load);
    console.log(look);
    console.log(ratio);
    console.log(average);
});

The results: The lookup time is on average a mere 8.68% of load time! Curiosity FTW.

Great work, Danny! The more you tinker with the API and your supporting tools, the more comfortable and confident you'll be with SuiteScript.