JavaScript coding trick!! For reduce execution time

Here we going to know some JavaScript coding trick or some function which can reduce our execution time.

Note: This tricks are learned from my day to day code

1. Object Assignment

//First Approach
let obj = {a: 1, b: 2, c: 3}

//Second Approach
let obj = JSON.parse('{"a":1,"b":2,"c":3}')

Here second approach is faster than first approach.

2. Switch Case and Object

For multiple if, else if case we generally write Switch cases. We can also replace switch case by object and object is faster than Switch case details...

//Swich case
function ab(month) {
    switch(month) {
      case 1: return 'Jan';
      case 2: return 'Feb';
              break;
      case 3: return 'Mar';
      default: return 'More than March';
    }
}

//Switch case by Object
function ab(month) {
    return {
      '1': 'Jan',
      '2' : 'Feb',
      '3': 'Mar',
    }[month] || 'More than March'
}

3. Get all Keys of an Object

We can get all keys from an Object by

  • For-in

  • Object.keys

let Obj = {a: 1, b: 2, c: 3};

//For-in
for(let key in Obj) {
  console.log(key)
}

//Object.keys
let keys = Object.keys(Obj);
console.log(keys)

Object.keys is faster than For-in

4. Delete key from an object or assign 'undefined' value

let obj1 = {
    a: 1,
    b: 2,
    c: 3
};

let obj2 = Object.create(obj1);
obj2['d'] = 4;

let obj3= Object.create(obj2);
obj3['e'] = 5;

Now, suppose we don't want key 'e', so we can achieve this

//Approach 1: delete key from the object
delete obj3['e'];

After above process, if we write

console.log(obj3['e']);
//then JavaScript search process will be
//obj3.__proto__.__proto__.__proto__.__proto__
// Above code can explain like obj3.__proto__, obj2.__proto__, obj1.__proto__, Object.__proto__

// After completion of all the step it will return "undefined"
//Approach 2: assign "undefined" value to the key
obj3['e'] = undefined;

Now if we write

console.log(obj3['e']); // undefined
//JavaScript will look into obj3
//and it found with undefined value

So, assign undefined is more effective then delete key from an Object

Note: JSON.stringify never process undefined key

5. Array.map Vs For-loop

Using simple integer array

let arr =[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30];

Iterate using map

let startTime = Date.now();
arr.map(d=> console.log(d));
let finishTime = Date.now();
console.log(startTime); // 1581838343955
console.log(finishTime); // 1581838343975
console.log(finishTime - startTime);// 20

Array.map took 20 millisecond for iterate the array

Iterate using For-loop

startTime = Date.now();

for(let i=0; i<arr.length; i+=1) {
  console.log(arr[i]);
}

finishTime = Date.now();

console.log(startTime); // 1581838665661
console.log(finishTime) // 1581838665679

console.log(finishTime - startTime); // 18

For-loop took 18 millisecond to execute.

Now examine above approach with a big array

arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30,
          1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,23,24,25,26,27,28,29,30];

Now Array.map() took 159 milliseconds and For-loop took 138 milliseconds.

Comments (1)

Mohd Shad Mirza's photo

Thanks for sharing. Cool tricks, 2nd and 4th ones were new to me.