javascript - what is better approach to use slice or splice when remove element from an array? -


because of high data volume have set array $scope.event size limit when reaches bufferlimit remove first item array , add latest item array. best approach use slice or splice in terms of high data volume when remove add item ?

ctrl.js

$scope.event = [];     function safelyadd(element) {         if (totalreceived > bufferlimit && $scope.event.length) {              $scope.event = $scope.event.slice(1); //delete first element in $scope.event              totalreceived -= $scope.event[0].messagesize; //total message size minus deleted message size             console.log('totalreceivedbytes', totalreceived);             // $scope.event =[];//reset array if max size reached..             console.log('$scope.event', $scope.event)         }         console.log('$scope.event.length', $scope.event.length);              $scope.event.push(element); //then push new item..      } 

that depends on requirements:

  1. the splice() method returns removed item(s) in array , slice() method returns selected element(s) in array, new array object.
  2. the splice() method changes original array , slice() method doesn't change original array.
  3. you can insert item in array while using splice() slice() removes item.

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -