lib/pararr.js
function executeParallel(op, arr, iter, cb) {
	var chunkSize = Math.floor(arr.length / numCPUs),
		worker,
		iterStr,
		task,
		offset,
		i;
	
	// Lazy initialization
	init();
	
	// Check params
	if (!cb) {
		throw Error('Expected callback');
	}	
    if (arr == null) {
		cb(null, []);
		return;
	}
	if (!Array.isArray(arr)) {
		cb(Error('Expected array'));
		return;
	}
	if (typeof iter !== 'function') {
		cb(Error('Expected iterator function'));
		return;
	}
	if (!isValidOP(op)) {
		cb(Error('Expected valid operation but got ' + op));
		return;
	}
	
	
	iterStr = iter.toString(); //Serialize iter
	
	for (i = 0; i < workers.length; i++) {
		worker = workers[i];
		offset = chunkSize * i;

		task = {
			type: 'func',
			op: op,
			data: (i === workers.length - 1 ? arr.slice(offset) : arr.slice(offset, offset + chunkSize)), // Partition arr
			iter: iterStr,
			context: {
				partition: i,
				jobID: jobID
			}
		};
		
		// Send task to worker
		// worker.send(task);
		buffer.add(task);
	}
	dispatchWorkItems();	

	// Store job
	jobs[jobID] = {
		type: 'func',
		result: [],
		cbCount: 0,
		cb: cb
	};
	
	// Increase jobID
	jobID++;
}
function executeParallel(op, arr, iter, cb) {
	var chunkSize = Math.floor(arr.length / numCPUs),
		worker,
		iterStr,
		task,
		offset,
		i;
	
	// Lazy initialization
	init();
	
	// Check params
	if (!cb) {
		throw Error('Expected callback');
	}	
    if (arr == null) {
		cb(null, []);
		return;
	}
	if (!Array.isArray(arr)) {
		cb(Error('Expected array'));
		return;
	}
	if (typeof iter !== 'function') {
		cb(Error('Expected iterator function'));
		return;
	}
	if (!isValidOP(op)) {
		cb(Error('Expected valid operation but got ' + op));
		return;
	}
	
	
	iterStr = iter.toString(); //Serialize iter
	
	for (i = 0; i < workers.length; i++) {
		worker = workers[i];
		offset = chunkSize * i;

		task = {
			type: 'func',
			op: op,
			data: (i === workers.length - 1 ? arr.slice(offset) : arr.slice(offset, offset + chunkSize)), // Partition arr
			iter: iterStr,
			context: {
				partition: i,
				jobID: jobID
			}
		};
		
		// Send task to worker
		// worker.send(task);
		buffer.add(task);
	}
	dispatchWorkItems();	

	// Store job
	jobs[jobID] = {
		type: 'func',
		result: [],
		cbCount: 0,
		cb: cb
	};
	
	// Increase jobID
	jobID++;
}
function executeParallel ( op , arr , iter , cb ) { var chunkSize = Math . floor ( arr . length / numCPUs ) , worker , iterStr , task , offset , i ; // Lazy initialization init ( ) ; // Check params if ( ! cb ) { throw Error ( 'Expected callback' ) ; } if ( arr == null ) { cb ( null , [ ] ) ; return ; } if ( ! Array . isArray ( arr ) ) { cb ( Error ( 'Expected array' ) ) ; return ; } if ( typeof iter !== 'function' ) { cb ( Error ( 'Expected iterator function' ) ) ; return ; } if ( ! isValidOP ( op ) ) { cb ( Error ( 'Expected valid operation but got ' + op ) ) ; return ; } iterStr = iter . toString ( ) ; //Serialize iter for ( i = 0 ; i < workers . length ; i ++ ) { worker = workers [ i ] ; offset = chunkSize * i ; task = { type : 'func' , op : op , data : ( i === workers . length - 1 ? arr . slice ( offset ) : arr . slice ( offset , offset + chunkSize ) ) , // Partition arr iter : iterStr , context : { partition : i , jobID : jobID } } ; // Send task to worker // worker.send(task); buffer . add ( task ) ; } dispatchWorkItems ( ) ; // Store job jobs [ jobID ] = { type : 'func' , result : [ ] , cbCount : 0 , cb : cb } ; // Increase jobID jobID ++ ; }
Execute an array operation in parallel
Execute an array operation in parallel