Node.js splicing is too slow for over 70,000 elements

I am new to node.js.

I tried inserting 70,000 elements into an array and then deleting all of them:

var Stopwatch = require("node-stopwatch").Stopwatch;
var stopwatch = Stopwatch.create();


var a = []
stopwatch.start();

for (var i = 1 ; i < 70000 ; i++){
    a.push((parseInt(Math.random() * 10000)) + "test");
}

for (var i = 1 ; i < 70000 ; i++){
    a.splice(0,1);
}

stopwatch.stop();

console.log("End: " + stopwatch.elapsedMilliseconds + " : " + a.length);
Run codeHide result

It works fine and outputs:

PS C:\Users\Documents\VSCode> node test.js
End: 51 : 0

But when I increase the number of elements to 72000, it takes too much time to complete:

var Stopwatch = require("node-stopwatch").Stopwatch;
var stopwatch = Stopwatch.create();


var a = []
stopwatch.start();

for (var i = 1 ; i < 72000 ; i++){
    a.push((parseInt(Math.random() * 10000)) + "test");
}

for (var i = 1 ; i < 72000 ; i++){
    a.splice(0,1);
}

stopwatch.stop();

console.log("End: " + stopwatch.elapsedMilliseconds + " : " + a.length);
Run codeHide result

And the result:

End: 9554 : 0

Why is this happening? only 2000 items added more, but it takes too much time.

Node.js version: v6.11.3

+6
source share
2 answers

V8 . ( ) ( array[0]) , . , .splice(0, 1):

for (var j = 0; j < a.length - 1; j++) {
  a[j] = a[j+1];
}
a.length = a.length - 1`

V8 , - , . . "" "" .

, ( array[array.length - 1]), . Array.pop(). , array.length = 0. FIFO/ "queue", , : "" , /, , , . :

function Queue() {
  this.enqueue = function(x) {
    this.array_.push(x);
  }
  this.dequeue = function() {
    var x = this.array_[this.cursor_++];
    // Free up space if half the array is unused.
    if (this.cursor_ > this.array_.length / 2) {
      this.array_.splice(0, this.cursor_);
      this.cursor_ = 0;
    }
    return x;
  }
  this.array_ = [];
  this.cursor_ = 0;
}

: , , 70 000 , 0: for (var i = 0; i < 70000; i++) {...}. , 69999 .

2: "parseInt" , double , . - Math.floor(Math.random() * 10000)). ( i.)

+3

, -

if (i % 1000 === 0) {
    console.log(i + " " + stopwatch.elapsedMilliseconds + " : " + a.length);
}

. ( , , )

. , , , " " 2000 - node. - - : [loop max num, unit, 3 resultsmarks results]

70k: [ms] ~ 26k, ~ 25.7k, ~ 26k 72k: [ms] ~ 25.6k, 27k, 25.7k

, , , 10k . , splice 1 , - 1 "", 10 10k-, , . :

var Stopwatch = require("node-stopwatch").Stopwatch;
var stopwatch = Stopwatch.create();

var a1 = [], a2 = [], a3 = [], a4 = [], a5 = [];
var a6 = [], a7 = [], a8 = [], a9 = [], a10 = [];

stopwatch.start();

function fill (arr) {
    for (var i = 1 ; i < 10000 ; i++){
        arr.push((parseInt(Math.random() * 10000)) + "test");
    }
}

fill(a1); fill(a2); fill(a3); fill(a4); fill(a5);
fill(a6); fill(a7); fill(a8); fill(a9); fill(a10);

let removeCount = 0;
function unfill(arr) {
    for (var i = 1 ; i < 10000 ; i++){
        arr.splice(0,1);
        removeCount++;

        if (i % 1000 === 0) {
            console.log(i + " " + stopwatch.elapsedMilliseconds + " : " + arr.length);
        }
    }
}

unfill(a1); unfill(a2); unfill(a3); unfill(a4); unfill(a5);
unfill(a6); unfill(a7); unfill(a8); unfill(a9); unfill(a10);

stopwatch.stop();

console.log("End: " + stopwatch.elapsedMilliseconds + " removeCount " + removeCount);

... , 70k 72k - , , ... , , - .

, . 100k (-10) 10 73-74 . , 2d- , , .

.

0

All Articles