denque
Advanced tools
Comparing version 1.2.2 to 1.2.3
@@ -13,3 +13,3 @@ 'use strict'; | ||
var l = 5000; | ||
var l = 100000; | ||
while (--l) { | ||
@@ -16,0 +16,0 @@ denque.push(l); |
21
index.js
@@ -7,11 +7,6 @@ 'use strict'; | ||
function Denque(array) { | ||
// circular buffer | ||
this._list = new Array(4); | ||
// bit mask | ||
this._capacityMask = 0x3; | ||
// next unread item | ||
this._head = 0; | ||
// next empty slot | ||
this._tail = 0; | ||
this._capacityMask = 0x3; | ||
this._list = new Array(4); | ||
if (Array.isArray(array)) { | ||
@@ -108,3 +103,3 @@ this._fromArray(array); | ||
Denque.prototype.unshift = function unshift(item) { | ||
if (item === undefined) return this.length; | ||
if (item === undefined) return this.size(); | ||
var len = this._list.length; | ||
@@ -138,3 +133,3 @@ this._head = (this._head - 1 + len) & this._capacityMask; | ||
Denque.prototype.push = function push(item) { | ||
if (item === undefined) return this.length; | ||
if (item === undefined) return this.size(); | ||
var tail = this._tail; | ||
@@ -294,3 +289,2 @@ this._list[tail] = item; | ||
var i = index; | ||
var size = this.size(); | ||
// expect a number or return undefined | ||
@@ -300,6 +294,5 @@ if ((i !== (i | 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; | ||
var size = this.size(); | ||
if (i < 0) i += size; | ||
if (i > size) return void 0; | ||
if (arguments.length > 2) { | ||
@@ -312,3 +305,3 @@ var k; | ||
var arguments_index = 2; | ||
if (i < size / 2) { | ||
if (!size || i < size / 2) { | ||
temp = new Array(i); | ||
@@ -315,0 +308,0 @@ for (k = 0; k < i; k++) { |
{ | ||
"name": "denque", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"description": "The fastest javascript implementation of a double-ended queue. Maintains compatability with deque.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# DENQUE | ||
[![Downloads](https://img.shields.io/npm/dm/denque.svg?style=flat-square)](https://www.npmjs.com/package/denque) | ||
[![npm version](https://img.shields.io/npm/v/denque.svg?style=flat-square)](https://www.npmjs.com/package/denque) | ||
[![Coverage Status](https://coveralls.io/repos/github/invertase/denque/badge.svg?branch=master)](https://coveralls.io/github/invertase/denque?branch=master) | ||
[![build](https://travis-ci.org/invertase/denque.svg)](https://travis-ci.org/invertase/denque) | ||
[![npm version](https://img.shields.io/npm/v/denque.svg)](https://www.npmjs.com/package/denque) | ||
[![Build](https://travis-ci.org/invertase/denque.svg)](https://travis-ci.org/invertase/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) | ||
Extremely fast and lightweight [double-ended queue](http://en.wikipedia.org/wiki/Double-ended_queue) implementation with zero dependencies. | ||
@@ -235,7 +236,3 @@ | ||
``` | ||
### 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> | ||
@@ -255,7 +252,3 @@ | ||
``` | ||
### 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> | ||
@@ -281,7 +274,3 @@ | ||
``` | ||
### 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> | ||
@@ -319,34 +308,41 @@ | ||
``` | ||
Darwin 16.5.0 x64 | ||
Node.JS 8.1.2 | ||
V8 5.8.283.41 | ||
Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz × 8 | ||
Darwin 17.0.0 x64 | ||
Node.JS 9.4.0 | ||
V8 6.2.414.46-node.17 | ||
Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz × 8 | ||
``` | ||
#### 1000 items in queue (3x shift + 3x push ops per 'op') | ||
(3x shift + 3x push ops per 'op') | ||
#### 1000 items in queue | ||
denque x 26,076,776 ops/sec ±0.76% (90 runs sampled) | ||
double-ended-queue x 15,347,410 ops/sec ±2.02% (87 runs sampled) | ||
(3 x shift + 3 x push ops per 'op') | ||
denque x 64,365,425 ops/sec ±0.69% (92 runs sampled) | ||
double-ended-queue x 26,646,882 ops/sec ±0.47% (94 runs sampled) | ||
#### 2 million items in queue | ||
(3x shift + 3x push ops per 'op') | ||
(3 x shift + 3 x push ops per 'op') | ||
denque x 23,267,668 ops/sec ±0.77% (88 runs sampled) | ||
double-ended-queue x 14,990,494 ops/sec ±1.18% (88 runs sampled) | ||
denque x 61,994,249 ops/sec ±0.26% (95 runs sampled) | ||
double-ended-queue x 26,363,500 ops/sec ±0.42% (91 runs sampled) | ||
#### Splice | ||
denque.splice x 288,034 ops/sec ±111.88% (76 runs sampled) | ||
native array splice x 6,112 ops/sec ±6.80% (54 runs sampled) | ||
(1 x splice per 'op') - initial size of 100,000 items | ||
denque.splice x 925,749 ops/sec ±22.29% (77 runs sampled) | ||
native array splice x 7,777 ops/sec ±8.35% (50 runs sampled) | ||
#### Remove | ||
denque.remove x 1,433,848 ops/sec ±0.54% (89 runs sampled) | ||
native array splice x 17,843 ops/sec ±58.22% (10 runs sampled) | ||
(1 x remove + 10 x push per 'op') - initial size of 100,000 items | ||
denque.remove x 2,635,275 ops/sec ±0.37% (95 runs sampled) | ||
native array splice - Fails to complete: "JavaScript heap out of memory" | ||
#### Remove One | ||
denque.removeOne x 973,777 ops/sec ±1.19% (84 runs sampled) | ||
native array splice x 70,916 ops/sec ±1.66% (84 runs sampled) | ||
(1 x removeOne + 10 x push per 'op') - initial size of 100,000 items | ||
denque.removeOne x 1,088,240 ops/sec ±0.21% (93 runs sampled) | ||
native array splice x 5,300 ops/sec ±0.41% (96 runs sampled) |
@@ -712,3 +712,3 @@ 'use strict'; | ||
assert.deepEqual(a.toArray(), b); | ||
assert.deepEqual(a.splice(a.length-1,2), b.splice(b.length-1,2)); //remove | ||
assert.deepEqual(a.splice(a.length - 1, 2), b.splice(b.length - 1, 2)); //remove | ||
assert.deepEqual(a.toArray(), b); | ||
@@ -782,2 +782,32 @@ }); | ||
}); | ||
it("Should remove and add items like native splice method to the empty denque", function () { | ||
var a = new Denque(); | ||
assert.deepEqual(a.splice(0, 0, 1), []); | ||
assert.deepEqual(a.toArray(), [1]); | ||
a.clear(); | ||
assert.deepEqual(a.splice(0, 0, 1, 2, 3, 4, 5), []); | ||
assert.deepEqual(a.toArray(), [1, 2, 3, 4, 5]); | ||
a.clear(); | ||
assert.deepEqual(a.splice(0, 1, 1, 2), void 0); // try to add and remove together | ||
assert.deepEqual(a.toArray(), [1, 2]); | ||
var b = new Denque([]); // initialized via empty array | ||
assert.deepEqual(b.splice(0, 0, 1), []); | ||
assert.deepEqual(b.toArray(), [1]) | ||
}); | ||
it("pop should shrink array when mostly empty", function () { | ||
var a = new Denque(); | ||
for (var i = 0; i < 50000; i++) { | ||
a.push(i); | ||
} | ||
var maskA = a._capacityMask; | ||
for (var ii = 0; ii < 35000; ii++) { | ||
a.pop(); | ||
} | ||
var maskB = a._capacityMask; | ||
assert(maskA > maskB); | ||
}); | ||
}); |
1310
48166
12
345