kriskowal/asap — pull request #23 by petkaantonov (2013-10-10 09:02:07+00:00)
Title: Use more efficient data structure

This patch changes asap to use a deque as a backing data structure.

From code comment:

```js
// Deque is a circular buffer with good locality of reference and doesn't
// allocate new memory unless there are more than 1024 parallel
// tasks in which case it will resize itself generously to x8 more
// capacity. The use case of asap should require no or few
// amount of resizes during runtime.

// Calling a task frees a slot immediately so if the calling
// has a side effect of queuing itself again, it can be sustained
// without additional memory
```

Here's the example that can now run forever, without using any additional memory as it runs:

```js
var asap = require("./asap");

function loop() {
asap(loop);
}
loop();
```

As I understand it, the above would at the moment eventually crash the process from out of memory error.

Here's a jsperf that explains why I didn't just use array http://jsperf.com/deque-vs-array-2/2.

The node tests passed but I get some weird error in zuul:

$ npm test

> asap@1.0.0 test c:\Users\Petka Antonov\asap
> npm run node-test && npm run phantomjs-test

> asap@1.0.0 node-test c:\Users\Petka Antonov\asap
> mocha test/tests.js

............

12 passing (7s)

> asap@1.0.0 phantomjs-test c:\Users\Petka Antonov\asap
> zuul test/tests.js

c:\Users\Petka Antonov\asap\node_modules\zuul\node_modules\browserify\node_modules\umd\node_modules\uglify-js\lib\parse.js:53
KEYWORDS = makePredicate(KEYWORDS);
^
util.debug: Use console.error instead
DEBUG: ERROR in file: c:\Users\Petka Antonov\asap\node_modules\zuul\node_modules\browserify\node_modules\umd\node_modules\uglify-js\lib\parse.js / ReferenceError: KEYWORDS is not defined
npm ERR! weird error 1
npm ERR! not ok code 0
npm ERR! weird error 1
npm ERR! not ok code 0