0

Is there some data-type like array that contains only last 100 elements? Or How to do this on my own?

We can just slice array from 0 to x element, when array length will be more than 100, but this is inefficient.

ElSajko
  • 1,612
  • 3
  • 17
  • 37
  • 1
    in other words, you want an array that at most has 100 items, and as more items are added, the oldest ones get removed. – Kevin B Jul 13 '16 at 21:36
  • There is no built-in array type like that. How you are items being added to the array. You could always build your own function/method for adding a new item to the array that would trim it if it was too long. – jfriend00 Jul 13 '16 at 21:36
  • This was already asked a day or two – Matías Fidemraizer Jul 13 '16 at 21:36
  • 1
    http://stackoverflow.com/questions/14236515/can-i-limit-the-length-of-an-array-in-javascript http://stackoverflow.com/questions/38242891/limit-array-size – Kevin B Jul 13 '16 at 21:38
  • @KevinB yes, we could just trim array at the begining every time it will has more than 100(n) but this is inefficient. Maybe linked-array may fit, you just remove x elements from begining and reassign main pointer. But how to do this in JS? – ElSajko Jul 13 '16 at 21:38
  • trimming would likely be the better option. It actually sounds like you are describing trimming. removing x elements from beginning of array, and then add that many to end. Surely creating a new array every time would be worse... – Kevin B Jul 13 '16 at 21:39
  • Inefficient compared to C/C++? JavaScript doesn't allow you to mess with pointers, really. – Eugene Kulabuhov Jul 13 '16 at 21:45

2 Answers2

2

You could use a ring buffer:

var n = 100;
var a = new Array(n);
var i = 0;

function push(x) {
   i = (i + 1) % n;
   a[i] = x;
}
emulbreh
  • 3,421
  • 23
  • 27
  • I just suggested using a Circular Queue as well and received a downvote immediately :P – Jack Ryan Jul 13 '16 at 21:47
  • @JackRyan the difference here is this answer is an answer, while yours was simply a sign post... to java docs... this is javascript. – Kevin B Jul 13 '16 at 21:51
  • Oh thanks @Kevin B! I must have made the classic mistake of thinking that Java === JavaScript. – Jack Ryan Jul 13 '16 at 21:56
0

You could use push and shift:

var a = [];

function append(value) {
  a.push(value);
  while (a.length > 10) {
    a.shift();
  }
}

for (var i = 0; i < 75; i++) {
  append(i);
}

console.log(a);

// Output:
// [ 65, 66, 67, 68, 69, 70, 71, 72, 73, 74 ]
user94559
  • 59,196
  • 6
  • 103
  • 103
  • inefficient when you do this a lot – ElSajko Jul 13 '16 at 21:39
  • 1
    @ElSajko As compared to what? Can you share your benchmarking methodology? – user94559 Jul 13 '16 at 21:41
  • @ElSajko If you're after performance, you may also need to specify which JavaScript engine you're using (browser or otherwise). The performance of methods like push and shift vary significantly depending on the implementation. – user94559 Jul 13 '16 at 21:42
  • hmm, I've changed my rate for your post, it may fit, at the begining I was thinking you've used splice instead of shift. But What if you want to add more than one element at once, then you have to use splice and splice creates new array each time. – ElSajko Jul 13 '16 at 21:44
  • you can push more than one element at a time, and the code in this answer would then trim it back to the max. you would just have to slightly modify the .push() line to support multiple in whichever form you want. – Kevin B Jul 13 '16 at 21:45
  • @KevinB ya, it's truth, but then you have to use splice instead of shift, splice creates new array each time – ElSajko Jul 13 '16 at 21:46
  • Why would you need splice? if you add 5 items, call shift 5 times. – Kevin B Jul 13 '16 at 21:46
  • @KevinB how would you trim 120 array to 100 array by not using splice? loops sound like inefficient way too :P – ElSajko Jul 13 '16 at 21:47
  • Right, but what you are wanting isn't implemented natively, you have to make a sacrifice somewhere. either splice and thus make new arrays every time you push, or loop and shift. – Kevin B Jul 13 '16 at 21:48