Category Archives: Stack Overflow

Answer by Nasser Torabzade for how do I measure the load time of a large array?

you can always use console.time('timerName') and console.timeEnd('timerName') to set a timer and find out elapsed time between two points of your javascript code, then compare the results:

console.time('BigArray');
var arr1 = [];
for(var i=0; i<200000; i++){
    arr1.push('test');
}
console.timeEnd('BigArray');


console.time('SeveralArrays');
for(var i=0; i<200000; i++){
    var arr2 = ['test'];
}
console.timeEnd('SeveralArrays');

the output will be something like:

BigArray: 123ms
SeveralArrays: 456ms

Answer by Nasser Torabzade for how do I measure the load time of a large array?

you can always use console.time('timerName') and console.timeEnd('timerName') to set a timer and find out elapsed time between two points of your javascript code, then compare the results:

console.time('BigArray');
var arr1 = [];
for(var i=0; i<200000; i++){
    arr1.push('test');
}
console.timeEnd('BigArray');


console.time('SeveralArrays');
for(var i=0; i<200000; i++){
    var arr2 = ['test'];
}
console.timeEnd('SeveralArrays');

the output will be something like:

BigArray: 123ms
SeveralArrays: 456ms

Answer by Nasser Torabzade for how do I measure the load time of a large array?

you can always use console.time('timerName') and console.timeEnd('timerName') to set a timer and find out elapsed time between two points of your javascript code, then compare the results:

console.time('BigArray');
var arr1 = [];
for(var i=0; i<200000; i++){
    arr1.push('test');
}
console.timeEnd('BigArray');


console.time('SeveralArrays');
for(var i=0; i<200000; i++){
    var arr2 = ['test'];
}
console.timeEnd('SeveralArrays');

the output will be something like:

BigArray: 123ms
SeveralArrays: 456ms

Answer by Nasser Torabzade for "Submit" Form and Direct to Javascript Generated URL

Instead of opening php page via javascript, you need to change the form action dynamically to point to your generated url.

HTML:

<form name="login">
    Username: <input type="text" name="user_id"/>
    Password: <input type="password" name="pswrd"/>
    <input type="submit" onclick="check(this.form)" value="Login"/>
</form>

JavaScript: (line 9 & 10 changed)

function check(form) {
    var userCredentials = [["jsmith", "smithpassword", "John Smith"], ["jdoe", "doepassword", "Jane Doe"]];
    var credCheck = 0;
    for (var i = 0; i < userCredentials.length; i++) {
        if (userCredentials[i][0] == form.user_id.value) {
            credCheck += 1;
            var displayName = userCredentials[i][2];
            if (userCredentials[i][1] == form.pswrd.value) {
                form.action = "home.php?display_name=" + displayName;
                return true;
            } else {
                alert('The username and password do not match.');
                return false;
            }
        }
    }
    if (credCheck == 0) {
        alert('The username entered is not valid.');
        return false;
    } else {
        return true;
    }
}

Answer by Nasser Torabzade for "Submit" Form and Direct to Javascript Generated URL

Instead of opening php page via javascript, you need to change the form action dynamically to point to your generated url.

HTML:

<form name="login">
    Username: <input type="text" name="user_id"/>
    Password: <input type="password" name="pswrd"/>
    <input type="submit" onclick="check(this.form)" value="Login"/>
</form>

JavaScript: (line 9 & 10 changed)

function check(form) {
    var userCredentials = [["jsmith", "smithpassword", "John Smith"], ["jdoe", "doepassword", "Jane Doe"]];
    var credCheck = 0;
    for (var i = 0; i < userCredentials.length; i++) {
        if (userCredentials[i][0] == form.user_id.value) {
            credCheck += 1;
            var displayName = userCredentials[i][2];
            if (userCredentials[i][1] == form.pswrd.value) {
                form.action = "home.php?display_name=" + displayName;
                return true;
            } else {
                alert('The username and password do not match.');
                return false;
            }
        }
    }
    if (credCheck == 0) {
        alert('The username entered is not valid.');
        return false;
    } else {
        return true;
    }
}