I'm loading a .txt file located on my server with this simple method:
function getFileFromServer(url, doneCallback)
{
console.time("getFileFromServer");
var xhr;
xhr = new XMLHttpRequest();
xhr.onreadystatechange = handleStateChange;
xhr.open("GET", url, true);
xhr.send();
function handleStateChange() {
if (xhr.readyState === 4) {
doneCallback(xhr.status == 200 ? xhr.responseText : null);
}
}
console.timeEnd("getFileFromServer");
}
I'm using it in this simple way, accordingly to this post: Reading a txt file from Server, writing it to website
function loadFile( url ) {
console.time("loadFile");
getFileFromServer(url, function(text) {
if (text === null) {
console.log("error-loading-file");
}
else {
console.timeEnd("loadFile");
doStuff(text);
}
});
As you can see I've put the console.time to keep track of the timing. Here's what the browser console answer:
getFileFromServer: 1.744ms
loadFile: 18114.871ms
I'm not a javascript expert, and the only thing I can figure out to explain the timing difference is the argument passing(value vs reference in C++). Someone can explain me this difference in timing?