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:
- the
splice()
method returns removed item(s) in array ,slice()
method returns selected element(s) in array, new array object. - the
splice()
method changes original array ,slice()
method doesn't change original array. - you can insert item in array while using
splice()
slice()
removes item.
Comments
Post a Comment