Queue Feature Request
Oriol _
oriol-bugzilla at hotmail.com
Sat Feb 25 23:27:54 UTC 2017
Hello,
you can try using a Map with unique keys. Map operations are guaranteed to be sublinear on average, and new entries are added to the end.
Then you only need a trivial wrapper for better abstraction:
```js
class Queue {
constructor() {
this._map = new Map();
}
push(value) {
this._map.set({}, value);
}
pop() {
var first = this._map.entries().next();
if (first.done) return;
this._map.delete(first.value[0]);
return first.value[1];
}
get size() {
return this._map.size;
}
}
var q = new Queue();
q.push(1);
q.push(2);
q.pop(); // 1
q.push(3);
q.pop(); // 2
q.pop(); // 3
q.pop(); // undefined
```
-- Oriol
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170225/81574f46/attachment.html>
More information about the es-discuss
mailing list