Permalink
Browse files

initial

  • Loading branch information...
0 parents commit 2dfa14f0cd3e4b40c73de61236b7a31eb6866fe7 @ybogdanov committed Apr 2, 2011
Showing with 719 additions and 0 deletions.
  1. +54 −0 README.md
  2. +34 −0 examples/push.js
  3. +34 −0 examples/push5.js
  4. +43 −0 examples/pushAll.js
  5. +43 −0 examples/pushAll5.js
  6. +244 −0 lib/narrow.js
  7. +114 −0 lib/test.js
  8. +13 −0 package.json
  9. +127 −0 test/base.js
  10. +13 −0 test/index.js
@@ -0,0 +1,54 @@
+
+# Introduction
+node-narrow is a library which shrinks a given callback parallel execution in a limited number of threads, receiving the bunch of data (array of tasks).
+
+Inspired from [node-async#queue](https://github.com/caolan/async#queue).
+
+# Synopsis
+Push a big bunch of data and handle it in maximum 5 simultaneous threads:
+
+ var Narrow = require('narrow');
+
+ var narrow = new Narrow(5, function(str, callback){
+ setTimeout(function(){
+ callback(null, str.toUpperCase());
+ }, 1000)
+ })
+
+ var tasks = [];
+ for (var i = 0; i < 10; i++) {
+ tasks.push('something' + i);
+ }
+
+ var start = new Date;
+
+ narrow.on('complete', function(result){
+ console.log('%s after %dms', result, new Date - start);
+ });
+
+ narrow.pushAll(tasks, function(){
+ console.log('done');
+ })
+
+Will output:
+
+ SOMETHING0 after 1002ms
+ SOMETHING1 after 1012ms
+ SOMETHING2 after 1012ms
+ SOMETHING3 after 1012ms
+ SOMETHING4 after 1012ms
+ SOMETHING5 after 2012ms
+ SOMETHING6 after 2012ms
+ SOMETHING7 after 2013ms
+ SOMETHING8 after 2016ms
+ SOMETHING9 after 2016ms
+ done
+
+Timeouts support
+
+ todo: document
+
+See more examples in [examples](https://github.com/0ctave/node-narrow/tree/master/examples) directory.
+
+# Installation
+ npm install narrow
@@ -0,0 +1,34 @@
+
+require.paths.unshift(__dirname + '/../lib');
+
+var Narrow = require('narrow');
+
+/**
+ * in 1 thread
+ */
+var narrow = new Narrow(function(str, callback){
+ setTimeout(function(){
+ callback(null, str.toUpperCase());
+ }, 1000)
+})
+
+var start = new Date;
+for (var i = 0; i < 10; i++) {
+ narrow.push('something' + i, function(err, result){
+ console.log('%s after %dms', result, new Date - start);
+ });
+}
+
+/* output:
+
+ SOMETHING0 after 1002ms
+ SOMETHING1 after 2012ms
+ SOMETHING2 after 3012ms
+ SOMETHING3 after 4012ms
+ SOMETHING4 after 5012ms
+ SOMETHING5 after 6016ms
+ SOMETHING6 after 7016ms
+ SOMETHING7 after 8016ms
+ SOMETHING8 after 9016ms
+ SOMETHING9 after 10016ms
+*/
@@ -0,0 +1,34 @@
+
+require.paths.unshift(__dirname + '/../lib');
+
+var Narrow = require('narrow');
+
+/**
+ * in 5 threads
+ */
+var narrow = new Narrow(5, function(str, callback){
+ setTimeout(function(){
+ callback(null, str.toUpperCase());
+ }, 1000)
+})
+
+var start = new Date;
+for (var i = 0; i < 10; i++) {
+ narrow.push('something' + i, function(err, result){
+ console.log('%s after %dms', result, new Date - start);
+ });
+}
+
+/* output:
+
+ SOMETHING0 after 1001ms
+ SOMETHING1 after 1012ms
+ SOMETHING2 after 1012ms
+ SOMETHING3 after 1012ms
+ SOMETHING4 after 1012ms
+ SOMETHING5 after 2016ms
+ SOMETHING6 after 2017ms
+ SOMETHING7 after 2017ms
+ SOMETHING8 after 2017ms
+ SOMETHING9 after 2017ms
+*/
@@ -0,0 +1,43 @@
+
+require.paths.unshift(__dirname + '/../lib');
+
+var Narrow = require('narrow');
+
+/**
+ * in 1 thread
+ */
+var narrow = new Narrow(function(str, callback){
+ setTimeout(function(){
+ callback(null, str.toUpperCase());
+ }, 1000)
+})
+
+var tasks = [];
+for (var i = 0; i < 10; i++) {
+ tasks.push('something' + i);
+}
+
+var start = new Date;
+
+narrow.on('complete', function(result){
+ console.log('%s after %dms', result, new Date - start);
+});
+
+narrow.pushAll(tasks, function(){
+ console.log('done');
+})
+
+/* output:
+
+ SOMETHING0 after 1003ms
+ SOMETHING1 after 2013ms
+ SOMETHING2 after 3013ms
+ SOMETHING3 after 4013ms
+ SOMETHING4 after 5013ms
+ SOMETHING5 after 6013ms
+ SOMETHING6 after 7017ms
+ SOMETHING7 after 8017ms
+ SOMETHING8 after 9017ms
+ SOMETHING9 after 10017ms
+ done
+*/
@@ -0,0 +1,43 @@
+
+require.paths.unshift(__dirname + '/../lib');
+
+var Narrow = require('narrow');
+
+/**
+ * in 5 threads
+ */
+var narrow = new Narrow(5, function(str, callback){
+ setTimeout(function(){
+ callback(null, str.toUpperCase());
+ }, 1000)
+})
+
+var tasks = [];
+for (var i = 0; i < 10; i++) {
+ tasks.push('something' + i);
+}
+
+var start = new Date;
+
+narrow.on('complete', function(result){
+ console.log('%s after %dms', result, new Date - start);
+});
+
+narrow.pushAll(tasks, function(){
+ console.log('done');
+})
+
+/* output:
+
+ SOMETHING0 after 1002ms
+ SOMETHING1 after 1012ms
+ SOMETHING2 after 1012ms
+ SOMETHING3 after 1012ms
+ SOMETHING4 after 1012ms
+ SOMETHING5 after 2012ms
+ SOMETHING6 after 2012ms
+ SOMETHING7 after 2013ms
+ SOMETHING8 after 2016ms
+ SOMETHING9 after 2016ms
+ done
+*/
Oops, something went wrong.

0 comments on commit 2dfa14f

Please sign in to comment.