Socket
Socket
Sign inDemoInstall

denque

Package Overview
Dependencies
0
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.1 to 1.2.0

benchmark/remove.js

187

index.js

@@ -165,2 +165,189 @@ 'use strict';

/**
* Remove and return the item at the specified index from the list.
* Returns undefined if the list is empty.
* @param index
* @returns {*}
*/
Denque.prototype.removeOne = function removeOne(index) {
var i = index;
// expect a number or return undefined
if ((i !== (i | 0))) {
return void 0;
}
if (this._head === this._tail) return void 0;
var size = this.size();
var len = this._list.length;
if (i >= size || i < -size) return void 0;
if (i < 0) i += size;
i = (this._head + i) & this._capacityMask;
var item = this._list[i];
var k;
if (index < size / 2) {
for (k = index; k > 0; k--) {
this._list[i] = this._list[i = (i - 1 + len) & this._capacityMask];
}
this._list[i] = void 0;
this._head = (this._head + 1 + len) & this._capacityMask;
} else {
for (k = size - 1 - index; k > 0; k--) {
this._list[i] = this._list[i = ( i + 1 + len) & this._capacityMask];
}
this._list[i] = void 0;
this._tail = (this._tail - 1 + len) & this._capacityMask;
}
return item;
};
/**
* Remove number of items from the specified index from the list.
* Returns array of removed items.
* Returns undefined if the list is empty.
* @param index
* @param count
* @returns {array}
*/
Denque.prototype.remove = function remove(index, count) {
var i = index;
var removed;
var del_count = count;
// expect a number or return undefined
if ((i !== (i | 0))) {
return void 0;
}
if (this._head === this._tail) return void 0;
var size = this.size();
var len = this._list.length;
if (i >= size || i < -size || count < 1) return void 0;
if (i < 0) i += size;
if (count === 1 || !count) {
removed = new Array(1);
removed[0] = this.removeOne(i);
return removed;
}
if (i === 0 && i + count >= size) return this.clear();
if (i + count > size) count = size - i;
var k;
removed = new Array(count);
for (k = 0; k < count; k++) {
removed[k] = this._list[(this._head + i + k) & this._capacityMask];
}
i = (this._head + i) & this._capacityMask;
if (index + count === size) {
this._tail = (this._tail - count + len) & this._capacityMask;
for (k = count; k > 0; k--) {
this._list[i = (i + 1 + len) & this._capacityMask] = void 0;
}
return removed;
}
if (index === 0) {
this._head = (this._head + count + len) & this._capacityMask;
for (k = count - 1; k > 0; k--) {
this._list[i = (i + 1 + len) & this._capacityMask] = void 0;
}
return removed;
}
if (index < size / 2) {
this._head = (this._head + index + count + len) & this._capacityMask;
for (k = index; k > 0; k--) {
this.unshift(this._list[i = (i - 1 + len) & this._capacityMask]);
}
i = (this._head - 1 + len) & this._capacityMask;
while (del_count > 0) {
this._list[i = (i - 1 + len) & this._capacityMask] = void 0;
del_count--;
}
} else {
this._tail = i;
i = (i + count + len) & this._capacityMask;
for (k = size - (count + index); k > 0; k--) {
this.push(this._list[i++]);
}
i = this._tail;
while (del_count > 0) {
this._list[i = (i + 1 + len) & this._capacityMask] = void 0;
del_count--;
}
}
if (this._head < 2 && this._tail > 10000 && this._tail <= len >>> 2) this._shrinkArray();
return removed;
};
/**
* Native splice implementation.
* Remove number of items from the specified index from the list and/or add new elements.
* Returns array of removed items or empty array if count == 0.
* Returns undefined if the list is empty.
*
* @param index
* @param count
* @param {...*} [elements]
* @returns {array}
*/
Denque.prototype.splice = function splice(index, count) {
var i = index;
var size = this.size();
// expect a number or return undefined
if ((i !== (i | 0))) {
return void 0;
}
if (this._head === this._tail) return void 0;
if (i > size || i < -size) return void 0;
if (i === size && count != 0) return void 0;
if (i < 0) i += size;
if (arguments.length > 2) {
var k;
var temp;
var removed;
var arg_len = arguments.length;
var len = this._list.length;
var arguments_index = 2;
if (i < size / 2) {
temp = new Array(i);
for (k = 0; k < i; k++) {
temp[k] = this._list[(this._head + k) & this._capacityMask];
}
if (count === 0) {
removed = [];
if (i > 0) {
this._head = (this._head + i + len) & this._capacityMask;
}
} else {
removed = this.remove(i, count);
this._head = (this._head + i + len) & this._capacityMask;
}
while (arg_len > arguments_index) {
this.unshift(arguments[--arg_len]);
}
for (k = i; k > 0; k--) {
this.unshift(temp[k - 1]);
}
} else {
temp = new Array(size - (i + count));
var leng = temp.length;
for (k = 0; k < leng; k++) {
temp[k] = this._list[(this._head + i + count + k) & this._capacityMask];
}
if (count === 0) {
removed = [];
if (i != size) {
this._tail = (this._head + i + len) & this._capacityMask;
}
} else {
removed = this.remove(i, count);
this._tail = (this._tail - leng + len) & this._capacityMask;
}
while (arguments_index < arg_len) {
this.push(arguments[arguments_index++]);
}
for (k = 0; k < leng; k++) {
this.push(temp[k]);
}
}
return removed;
} else {
return this.remove(i, count);
}
};
/**
* Soft clear - does not reset capacity.

@@ -167,0 +354,0 @@ */

8

package.json
{
"name": "denque",
"version": "1.1.1",
"version": "1.2.0",
"description": "The fastest javascript implementation of a double-ended queue. Maintains compatability with deque.",
"main": "index.js",
"engines": {
"node": ">=4.0"
"node": ">=0.10"
},

@@ -24,3 +24,5 @@ "keywords": [

"benchmark_thousand": "node benchmark/thousand",
"benchmark_two_million": "node benchmark/two_million"
"benchmark_splice": "node benchmark/splice",
"benchmark_remove": "node benchmark/remove",
"benchmark_removeOne": "node benchmark/removeOne"
},

@@ -27,0 +29,0 @@ "repository": {

@@ -7,2 +7,3 @@ # DENQUE

[![License](https://img.shields.io/npm/l/denque.svg)](/LICENSE)
[![Donate](https://img.shields.io/badge/Donate-Patreon-green.svg)](https://www.patreon.com/invertase)

@@ -47,6 +48,9 @@ Extremely fast and lightweight [double-ended queue](http://en.wikipedia.org/wiki/Double-ended_queue) implementation with zero dependencies.

- [`peekAt(int index)`](#peekAtint-index---dynamic)
- [`remove(int index, int count)`](#remove)
- [`removeOne(int index)`](#removeOne)
- [`splice(int index, int count, item1, item2, ...)`](#splice)
- [`isEmpty()`](#isempty---boolean)
- [`clear()`](#clear---void)
#####`new Denque()` -> `Denque`
#### `new Denque()` -> `Denque`

@@ -218,2 +222,68 @@ Creates an empty double-ended queue with initial capacity of 4.

#####`remove(int index, int count)` -> `array`
Remove number of items from the specified index from the list.
Returns array of removed items.
Returns undefined if the list is empty.
```js
var denque = new Denque([1,2,3,4,5,6,7]);
denque.remove(0,3); //[1,2,3]
denque.remove(1,2); //[5,6]
var denque1 = new Denque([1,2,3,4,5,6,7]);
denque1.remove(4, 100); //[5,6,7]
```
### 100000 items in queue performance
denque.remove x 649,195 ops/sec ±1.33% (83 runs sampled)
native array splice x 54,461 ops/sec ±164.33% (11 runs sampled)
<hr>
#####`removeOne(int index)` -> `dynamic`
Remove and return the item at the specified index from the list.
Returns undefined if the list is empty.
```js
var denque = new Denque([1,2,3,4,5,6,7]);
denque.removeOne(4); // 5
denque.removeOne(3); // 4
denque1.removeOne(1); // 2
```
### 100000 items in queue performance
denque.removeOne x 487,168 ops/sec ±0.94% (85 runs sampled)
native array splice x 39,082 ops/sec ±0.87% (88 runs sampled)
<hr>
#####`splice(int index, int count, item1, item2, ...)` -> `array`
Native splice implementation.
Remove number of items from the specified index from the list and/or add new elements.
Returns array of removed items or empty array if count == 0.
Returns undefined if the list is empty.
```js
var denque = new Denque([1,2,3,4,5,6,7]);
denque.splice(denque.length, 0, 8, 9, 10); // []
denque.toArray() // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
denque.splice(3, 3, 44, 55, 66); // [4,5,6]
denque.splice(5,4, 666,667,668,669); // [ 66, 7, 8, 9 ]
denque.toArray() // [ 1, 2, 3, 44, 55, 666, 667, 668, 669, 10 ]
```
### 100000 items in queue performance
denque.splice x 178,198 ops/sec ±8.68% (61 runs sampled)
native array splice x 3,639 ops/sec ±4.39% (65 runs sampled)
<hr>
#####`isEmpty()` -> `boolean`

@@ -220,0 +290,0 @@

@@ -62,3 +62,3 @@ 'use strict';

denque.shift();
if (l=== 25000) denque.clear();
if (l === 25000) denque.clear();
denque.pop();

@@ -341,3 +341,3 @@ denque.pop();

describe('Denque.prototype.peekFront', function () {
it("Should return undefined when queue is empty", function() {
it("Should return undefined when queue is empty", function () {
var a = new Denque();

@@ -364,3 +364,3 @@ assert(a.length === 0);

describe('Denque.prototype.peekBack', function () {
it("Should return undefined when queue is empty", function() {
it("Should return undefined when queue is empty", function () {
var a = new Denque();

@@ -397,1 +397,389 @@ assert(a.length === 0);

describe('Denque.prototype.removeOne', function () {
it("Should return undefined when empty denque", function () {
var a = new Denque();
assert(a.length === 0);
assert(a.removeOne(1) === void 0);
assert(a.length === 0);
});
it("Should return undefined when index is invalid", function () {
var a = new Denque();
var b = new Denque();
b.push('foobar');
b.push('foobaz');
assert(a.length === 0);
assert(a.removeOne('foobar') === void 0);
assert(b.removeOne(-1, 2) === 'foobaz');
assert(b.removeOne(-4, 0) === void 0);
assert(b.removeOne(3, 2) === void 0);
assert(a.removeOne({}) === void 0);
assert(a.length === 0);
});
});
describe('Denque.prototype.remove', function () {
it("Should return undefined when empty denque", function () {
var a = new Denque();
assert(a.length === 0);
assert(a.remove(1) === void 0);
assert(a.remove(2, 3) === void 0);
assert(a.length === 0);
});
it("Should return undefined if index or count invalid", function () {
var a = new Denque();
var b = new Denque();
b.push('foobar');
b.push('foobaz');
assert(a.length === 0);
assert(a.remove('foobar') === void 0);
assert(b.remove(-1, 0) === void 0);
assert(b.remove(-1, 2).length === 1);
assert(b.remove(-5, 1) === void 0);
assert(b.remove(66, 0) === void 0);
assert(a.remove({}) === void 0);
assert(a.length === 0);
});
it("Should return the item at the front of the denque", function () {
var a = new Denque([1, 2, 3, 4, 5, 6, 7, 8, 9]);
var b = [];
b.push(1, 2, 3, 4, 5, 6, 7, 8, 9);
assert.deepEqual(a.remove(0, 1), b.splice(0, 1));
assert.deepEqual(a.remove(0, 1), b.splice(0, 1));
assert.deepEqual(a.toArray(), b);
a.unshift(5);
a.unshift(4);
a.unshift(3);
a.unshift(2);
a.unshift(1);
a.push(1);
a.push(2);
a.push(3);
a.push(4);
a.push(5);
a.unshift(3);
a.unshift(2);
a.unshift(1);
a.remove(0, 1);
b.unshift(1, 2, 3, 4, 5);
b.push(1, 2, 3, 4, 5);
b.unshift(1, 2, 3);
b.splice(0, 1);
assert.deepEqual(a.toArray(), b);
assert.deepEqual(a.remove(0, 1), b.splice(0, 1));
assert.deepEqual(a.toArray(), b);
});
it("Should return the item at the back of the denque", function () {
var a = new Denque([1, 2, 3, 4, 5, 6, 7, 8, 9]);
var b = [];
b.push(1, 2, 3, 4, 5, 6, 7, 8, 9);
assert.deepEqual(a.remove(8, 1), b.splice(8, 1));
assert.deepEqual(a.toArray(), b);
a.unshift(5);
a.unshift(4);
a.unshift(3);
a.unshift(2);
a.unshift(1);
a.push(1);
a.push(2);
a.push(3);
a.push(4);
a.push(5);
a.unshift(3);
a.unshift(2);
a.unshift(1);
a.remove(20, 1);
b.unshift(1, 2, 3, 4, 5);
b.push(1, 2, 3, 4, 5);
b.unshift(1, 2, 3);
b.splice(20, 1);
assert.deepEqual(a.toArray(), b);
assert.deepEqual(a.remove(19, 1), b.splice(19, 1));
assert.deepEqual(a.toArray(), b);
});
it("Should return the item somewhere in the middle of the denque", function () {
var a = new Denque([1, 2, 3, 4, 5, 6, 7, 8, 9]);
var b = [];
b.push(1, 2, 3, 4, 5, 6, 7, 8, 9);
assert(a.remove(4, 1), b.splice(4, 1));
assert(a.remove(5, 1), b.splice(5, 1));
assert(a.remove(3, 1), b.splice(3, 1));
assert.deepEqual(a.toArray(), b);
a.unshift(5);
a.unshift(4);
a.unshift(3);
a.unshift(2);
a.unshift(1);
a.push(1);
a.push(2);
a.push(3);
a.push(4);
a.push(5);
a.unshift(3);
a.unshift(2);
a.unshift(1);
a.remove(7, 1);
b.unshift(1, 2, 3, 4, 5);
b.push(1, 2, 3, 4, 5);
b.unshift(1, 2, 3);
b.splice(7, 1);
assert.deepEqual(a.toArray(), b);
assert.deepEqual(a.remove(1, 4), b.splice(1, 4));
assert.deepEqual(a.toArray(), b);
});
it("Should remove a number of items at the front of the denque", function () {
var a = new Denque([1, 2, 3, 4, 5, 6, 7, 8, 9]);
var b = [];
b.push(1, 2, 3, 4, 5, 6, 7, 8, 9);
assert.deepEqual(a.remove(0, 5), b.splice(0, 5));
assert.deepEqual(a.toArray(), b);
a.unshift(5);
a.unshift(4);
a.unshift(3);
a.unshift(2);
a.unshift(1);
a.push(1);
a.push(2);
a.push(3);
a.push(4);
a.push(5);
a.unshift(3);
a.unshift(2);
a.unshift(1);
a.remove(0, 11);
b.unshift(1, 2, 3, 4, 5);
b.push(1, 2, 3, 4, 5);
b.unshift(1, 2, 3);
b.splice(0, 11);
assert.deepEqual(a.toArray(), b);
assert.deepEqual(a.remove(0, 1), b.splice(0, 1));
assert.deepEqual(a.toArray(), b);
});
it("Should remove a number of items at the back of the denque", function () {
var a = new Denque([1, 2, 3, 4, 5, 6, 7, 8, 9]);
var b = [];
b.push(1, 2, 3, 4, 5, 6, 7, 8, 9);
assert.deepEqual(a.remove(5, 4), b.splice(5, 4));
assert.deepEqual(a.toArray(), b);
a.unshift(5);
a.unshift(4);
a.unshift(3);
a.unshift(2);
a.unshift(1);
a.push(1);
a.push(2);
a.push(3);
a.push(4);
a.push(5);
a.unshift(3);
a.unshift(2);
a.unshift(1);
a.remove(16, 3);
b.unshift(1, 2, 3, 4, 5);
b.push(1, 2, 3, 4, 5);
b.unshift(1, 2, 3);
b.splice(16, 3);
assert.deepEqual(a.toArray(), b);
assert.deepEqual(a.remove(5, 100), b.splice(5, 100));
assert.deepEqual(a.toArray(), b);
});
it("Should remove a number of items somewhere in the middle of the denque", function () {
var a = new Denque([1, 2, 3, 4, 5, 6, 7, 8, 9]);
var b = [];
b.push(1, 2, 3, 4, 5, 6, 7, 8, 9);
assert.deepEqual(a.remove(3, 3), b.splice(3, 3));
assert.deepEqual(a.toArray(), b);
a.unshift(5);
a.unshift(4);
a.unshift(3);
a.unshift(2);
a.unshift(1);
a.push(1);
a.push(2);
a.push(3);
a.push(4);
a.push(5);
a.unshift(3);
a.unshift(2);
a.unshift(1);
// console.log(a.toArray())
a.remove(8, 6);
b.unshift(1, 2, 3, 4, 5);
b.push(1, 2, 3, 4, 5);
b.unshift(1, 2, 3);
b.splice(8, 6);
assert.deepEqual(a.toArray(), b);
assert.deepEqual(a.remove(3, 3), b.splice(3, 3));
assert.deepEqual(a.toArray(), b);
});
it("Should clear denque", function () {
var a = new Denque([1, 2, 3, 4, 5, 6, 7, 8, 9]);
var b = [];
b.push(1, 2, 3, 4, 5, 6, 7, 8, 9);
a.remove(0, 9);
b.splice(0, 9);
assert.deepEqual(a.toArray(), b)
});
});
describe('Denque.prototype.splice', function () {
it("Should remove and add items like native splice method at the front of denque", function () {
var a = new Denque([1, 2, 3, 4, 5, 6, 7, 8, 9]);
var b = [];
b.push(1, 2, 3, 4, 5, 6, 7, 8, 9);
assert.deepEqual(a.splice(0, 2, 14, 15, 16), [1, 2]); // remove then add
a.splice(0, 0, [11, 12, 13]); // add
b.splice(0, 2, 14, 15, 16);
b.splice(0, 0, [11, 12, 13]);
assert.deepEqual(a.toArray(), b);
a.unshift(5);
a.unshift(4);
a.unshift(3);
a.unshift(2);
a.unshift(1);
a.push(1);
a.push(2);
a.push(3);
a.push(4);
a.push(5);
a.unshift(3);
a.unshift(2);
a.unshift(1);
a.splice(0, 0, 17, 18, 19);
b.unshift(1, 2, 3, 4, 5);
b.push(1, 2, 3, 4, 5);
b.unshift(1, 2, 3);
b.splice(0, 0, 17, 18, 19);
assert.deepEqual(a.toArray(), b);
assert.deepEqual(a.splice(0, 2), b.splice(0, 2)); //remove
assert.deepEqual(a.toArray(), b);
});
it("Should remove and add items like native splice method at the end of denque", function () {
var a = new Denque([1, 2, 3, 4, 5, 6, 7, 8, 9]);
var b = [];
b.push(1, 2, 3, 4, 5, 6, 7, 8, 9);
assert.deepEqual(a.splice(a.length - 1, 1, 14, 15, 16), [9]); // remove then add
a.splice(a.length, 0, [11, 12, 13]); // add
b.splice(b.length - 1, 1, 14, 15, 16);
b.splice(b.length, 0, [11, 12, 13]);
assert.deepEqual(a.toArray(), b);
a.unshift(5);
a.unshift(4);
a.unshift(3);
a.unshift(2);
a.unshift(1);
a.push(1);
a.push(2);
a.push(3);
a.push(4);
a.push(5);
a.unshift(3);
a.unshift(2);
a.unshift(1);
a.splice(a.length, 0, 17, 18, 19);
b.unshift(1, 2, 3, 4, 5);
b.push(1, 2, 3, 4, 5);
b.unshift(1, 2, 3);
b.splice(b.length, 0, 17, 18, 19);
assert.deepEqual(a.toArray(), b);
a.splice(18, 0, 18, 19);
b.splice(18, 0, 18, 19);
assert.deepEqual(a.toArray(), b);
a.splice(21, 0, 1, 2, 3, 4, 5, 6);
b.splice(21, 0, 1, 2, 3, 4, 5, 6);
assert.deepEqual(a.toArray(), b);
assert.deepEqual(a.splice(a.length-1,2), b.splice(b.length-1,2)); //remove
assert.deepEqual(a.toArray(), b);
});
it("Should remove and add items like native splice method somewhere in the middle of denque", function () {
var a = new Denque([1, 2, 3, 4, 5, 6, 7, 8, 9]);
var b = [];
b.push(1, 2, 3, 4, 5, 6, 7, 8, 9);
a.splice(2, 0, [11, 12, 13]);
assert.deepEqual(a.splice(7, 2, 14, 15, 16), [7, 8]); // remove then add
assert.deepEqual(a.splice(10, 1, 17, 18), [9]);
b.splice(2, 0, [11, 12, 13]);
b.splice(7, 2, 14, 15, 16);
b.splice(10, 1, 17, 18);
assert.deepEqual(a.toArray(), b);
a.unshift(5);
a.unshift(4);
a.unshift(3);
a.unshift(2);
a.unshift(1);
a.push(1);
a.push(2);
a.push(3);
a.push(4);
a.push(5);
a.unshift(3);
a.unshift(2);
a.unshift(1);
b.unshift(1, 2, 3, 4, 5);
b.push(1, 2, 3, 4, 5);
b.unshift(1, 2, 3);
assert.deepEqual(a.splice(3, 3, 16, 15, 14), b.splice(3, 3, 16, 15, 14));
assert.deepEqual(a.toArray(), b);
assert.deepEqual(a.splice(6, 1), b.splice(6, 1));
assert.deepEqual(a.toArray(), b);
});
it("Should return undefined when index or count is invalid", function () {
var a = new Denque();
var b = new Denque();
b.push('foobar');
b.push('foobaz');
assert(a.length === 0);
assert(a.splice('foobar') === void 0);
assert(b.splice(-1, 0) === void 0);
assert(b.splice(-5, 1) === void 0);
assert(b.splice(66, 0) === void 0);
assert(a.splice({}) === void 0);
assert(a.length === 0);
});
it("Should return undefined when the queue is empty", function () {
var a = new Denque();
assert(a.length === 0);
assert(a.splice(1, 0) === void 0);
});
it("Should return undefined when trying to remove further than current size", function () {
var a = new Denque();
a.push('foobar');
a.push('foobar1');
a.push('foobar2');
a.push('foobar3');
assert(a.length === 4);
assert(a.splice(4, 234) === void 0);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc