如何度量将JSON字符串解析为Javascript对象的时间

时间:2022-08-26 11:58:53

I'm using AJAX query to get some long array from server side. The array is really big (let's say million elements), so even HTTP query takes a time, I see it in a console. But after the query is done, some time passes until I see an output which comes from a callback function of AJAX query.

我使用AJAX查询从服务器端获取一些长数组。数组非常大(假设有一百万个元素),因此即使是HTTP查询也要花费时间,我在控制台中看到它。但是,在查询完成之后,会过一段时间,直到看到来自AJAX查询的回调函数的输出。

I try to measure some time inside that function, but the time difference is miserable and doesn't look like referring to the whole process. (If I put time brackets outside AJAX querying function I get anyway a zero which is clear why) Inspecting of Firebug profiler results also didn't give me a glue..

我试着在这个函数里面测量一些时间,但是时差很糟糕,看起来不像是指整个过程。(如果我把时间括号放在AJAX查询函数之外,我就会得到一个0,这是很明显的原因)对Firebug分析器结果的检查也没有给我胶水。

Here is my code (I use jQuery):

这是我的代码(我使用jQuery):

$.getJSON(
    'some-url',
    '',
    function(data) {
        var start = (new Date).getTime();
        console.log(data.length);
        var end = (new Date).getTime();
        console.log((end - start) /1000); // for 1M array gives something like 0.03 s
    }
);

So I want to catch a time of the whole process happening in browser's engine related to dealing with that object. How can I do that?

我想了解一下浏览器引擎中与处理对象相关的整个过程。我怎么做呢?

3 个解决方案

#1


2  

Use ajax , not getJSON:

使用ajax,而不是getJSON:

$.ajax({
   url: 'some-url',
   dataType: 'text',
   success: function(jsonText) {           
       console.log(jsonText.length);
       var start = (new Date).getTime();

       var data = $.parseJSON(jsonText);  

       var end = (new Date).getTime();
       console.log((end - start) /1000); // for 1M array gives something like 0.03 s
   }
});

#2


1  

If I understand your answer correctly, you want to measure the time it takes to process the response. I looks like callback passed to $.getJSON is called after the response JSON is parsed. You will need to use a different method, perhaps $.ajax. The following code should also work (untested):

如果我正确地理解了您的答案,您希望度量处理响应所需的时间。看起来回调传递给了$。在解析响应JSON之后调用getJSON。您将需要使用另一种方法,可能是$.ajax。以下代码也应该有效(未经测试):

function initiateGetRequest(url, callback)
    var req;
    req = new XMLHttpRequest();
    req.open("GET", url, true);
    req.onreadystatechange = function() {
        if (req.readyState === 4) {
            callback(req.responseText, req.status);
        }
    };
    req.send(null);
}

function processMyJSONResponse(responseText, statusCode) {
    var startTime;        
    var myResponseObject;
    if (statusCode === 200) {
        startTime = Date.now();
        // Processing logic here
        myResponseObject = JSON.parse(responseText);

        console.log((Date.now() - startTime) / 1000);
    } else {
        console.log("Something went wrong, statusCode: " + statusCode);
    }
}

// somewhere else
initiateGetRequest("myUrl", processMyJSONResponse);

#3


0  

The answer is simple: create a jsPerf test case. It allows running asynchronous or “deferred” tests.

答案很简单:创建一个jsPerf测试用例。它允许运行异步或“延迟”测试。

Alternatively, you could use Benchmark.js and set up a test case manually.

或者,您可以使用Benchmark。然后手动设置一个测试用例。

Don’t simply compare two new Date timestamps, as this is not an accurate way of measuring things across all browsers and devices.

不要简单地比较两个新的日期时间戳,因为这不是在所有浏览器和设备上测量东西的准确方法。

#1


2  

Use ajax , not getJSON:

使用ajax,而不是getJSON:

$.ajax({
   url: 'some-url',
   dataType: 'text',
   success: function(jsonText) {           
       console.log(jsonText.length);
       var start = (new Date).getTime();

       var data = $.parseJSON(jsonText);  

       var end = (new Date).getTime();
       console.log((end - start) /1000); // for 1M array gives something like 0.03 s
   }
});

#2


1  

If I understand your answer correctly, you want to measure the time it takes to process the response. I looks like callback passed to $.getJSON is called after the response JSON is parsed. You will need to use a different method, perhaps $.ajax. The following code should also work (untested):

如果我正确地理解了您的答案,您希望度量处理响应所需的时间。看起来回调传递给了$。在解析响应JSON之后调用getJSON。您将需要使用另一种方法,可能是$.ajax。以下代码也应该有效(未经测试):

function initiateGetRequest(url, callback)
    var req;
    req = new XMLHttpRequest();
    req.open("GET", url, true);
    req.onreadystatechange = function() {
        if (req.readyState === 4) {
            callback(req.responseText, req.status);
        }
    };
    req.send(null);
}

function processMyJSONResponse(responseText, statusCode) {
    var startTime;        
    var myResponseObject;
    if (statusCode === 200) {
        startTime = Date.now();
        // Processing logic here
        myResponseObject = JSON.parse(responseText);

        console.log((Date.now() - startTime) / 1000);
    } else {
        console.log("Something went wrong, statusCode: " + statusCode);
    }
}

// somewhere else
initiateGetRequest("myUrl", processMyJSONResponse);

#3


0  

The answer is simple: create a jsPerf test case. It allows running asynchronous or “deferred” tests.

答案很简单:创建一个jsPerf测试用例。它允许运行异步或“延迟”测试。

Alternatively, you could use Benchmark.js and set up a test case manually.

或者,您可以使用Benchmark。然后手动设置一个测试用例。

Don’t simply compare two new Date timestamps, as this is not an accurate way of measuring things across all browsers and devices.

不要简单地比较两个新的日期时间戳,因为这不是在所有浏览器和设备上测量东西的准确方法。