Part 6 - Write Fast JavaScript

Sign in to queue

The Discussion

  • User profile image
    Charles

    Hi Dr Chen

    I went to check out how re-defined array size can help with the speed. I used edge to test. The code is as follows

    let size = 10000000;
    let a = new Array();

    let startTime1 = new Date();
    for(let i = 0; i< size; i++){
    a[i] = 'hi';
    }

    let endTime1 = new Date();
    let slow = endTime1.getTime() - startTime1.getTime();
    a = undefined;
    console.log(slow);

    as you can see, the next step I took was to actually pass "size" into the array declaration and see the console value. It turns out,the actually timing is around 6 seconds for both. The faster one is faster by 200ms.

    So my question is, in what situation will this perfomance actually make a difference? I tried to use 1 billion, the browser will just crash. Hence I don't see an actual case where pre-defined array size will make a difference.

  • User profile image
    matteocontr​ini

    This is not properly writing faster JavaScript. This is often creating a big mess with code that's less easy to understand and that won't probably pass some linters.

Add Your 2 Cents