Return to Snippet

Revision: 13557
at June 11, 2009 01:46 by jschilling


Updated Code
forEach = (function()
{
	/**
	* USAGE
        * instead of:
        * for(var i=0, il=some.length; i<il; i++)
        * Use this:
	* forEach(object, function(i){
	*	i.doSomething();
	* }, context);
        *
        * or this:
        * forEach(object, function(i){
        *       i.doSomething();
        * });
        *
	* @param object elementNodeList || array
	* @param block function
	* @param context inherited by the this object
	*/
	return function(object, block, context)
	{
		var len = Math.floor(object.length / 4),
			left = object.length % 4, i = 0;
		if(left > 0){
			do {
				block.call(context, object[i++], i, object);
			} while (--left > 0);
		};
		while(len-- > 0)
		{
			block.call(context, object[i++], i, object);
			block.call(context, object[i++], i, object);
			block.call(context, object[i++], i, object);
			block.call(context, object[i++], i, object);
		};
	};
})();

Revision: 13556
at May 2, 2009 18:24 by jschilling


Updated Code
forEach = (function()
{
	/**
	* USAGE
        * instead of:
        * for(var i=0, il=some.length; i<il; i++)
        * Use this:
	* forEach(object, function(i)
	*	i.doSomething;
	* ), context};
        *
	* @param object elementNodeList
	* @param block function
	* @param context inherited by the this object
	*/
	return function(object, block, context)
	{
		var len = Math.floor(object.length / 4),
			left = object.length % 4, i = 0;
		if(left > 0){
			do {
				block.call(context, object[i++], i, object);
			} while (--left > 0);
		};
		while(len-- > 0)
		{
			block.call(context, object[i++], i, object);
			block.call(context, object[i++], i, object);
			block.call(context, object[i++], i, object);
			block.call(context, object[i++], i, object);
		};
	};
})();

Revision: 13555
at April 28, 2009 17:59 by jschilling


Updated Code
var forEach = function(object, block, context)
{
	/**
	* USAGE
        * instead of:
        * for(var i=0, il=some.length; i<il; i++)
        * Use this:
	* forEach(object, function(o)
	*	o.doSomething;
	* ), context};
        *
	* @param object elementNodeList
	* @param block function
	* @param context inherited by the this object
	*/
	var len = Math.floor(object.length / 4),
		left = object.length % 4, i = 0;
	if(left > 0){
		do {
			block.call(context, object[i++], i, object);
		} while (--left > 0);
	}
	while(len-- > 0)
	{
		block.call(context, object[i++], i, object);
		block.call(context, object[i++], i, object);
		block.call(context, object[i++], i, object);
		block.call(context, object[i++], i, object);
	}
};

Revision: 13554
at April 28, 2009 17:57 by jschilling


Updated Code
var forEach = function(object, block, context)
{
	/**
	* USAGE
        * instead of:
        * for(var i=0, il=some.length; i<il; i++)
        * Use this:
	* forEach(object, function(o)
	*	o.doSomething;
	* ), thisArg};
        *
	* @param object elementNodeList
	* @param block function
	* @param context inherited by the this object
	*/
	var len = Math.floor(object.length / 4),
		left = object.length % 4, i = 0;
	if(left > 0){
		do {
			block.call(context, object[i++], i, object);
		} while (--left > 0);
	}
	while(len-- > 0)
	{
		block.call(context, object[i++], i, object);
		block.call(context, object[i++], i, object);
		block.call(context, object[i++], i, object);
		block.call(context, object[i++], i, object);
	}
};

Revision: 13553
at April 28, 2009 17:56 by jschilling


Updated Code
var forEach = function(object, block, context)
{
	/**
	* USAGE
        * instead of:
        * for(var i=0, il=<some.length; i<il; i++)
        * Use this:
	* forEach(object, function(o)
	*	o.doSomething;
	* ), thisArg};
        *
	* @param object elementNodeList
	* @param block function
	* @param context inherited by the this object
	*/
	var len = Math.floor(object.length / 4),
		left = object.length % 4, i = 0;
	if(left > 0){
		do {
			block.call(context, object[i++], i, object);
		} while (--left > 0);
	}
	while(len-- > 0)
	{
		block.call(context, object[i++], i, object);
		block.call(context, object[i++], i, object);
		block.call(context, object[i++], i, object);
		block.call(context, object[i++], i, object);
	}
};

Revision: 13552
at April 27, 2009 15:42 by jschilling


Initial Code
var forEach = function(object, block, context)
{
	/**
	* Usage:
	* forEach(object, function(o)
	*	o.doSomething;
	* ), thisArg};
	* @object elementNodeList
	* @block function
	* @context inherited by the this object
	*/
	var len = Math.floor(object.length / 4),
		left = object.length % 4, i = 0;
	if(left > 0){
		do {
			block.call(context, object[i++], i, object);
		} while (--left > 0);
	}
	while(len-- > 0)
	{
		block.call(context, object[i++], i, object);
		block.call(context, object[i++], i, object);
		block.call(context, object[i++], i, object);
		block.call(context, object[i++], i, object);
	}
};

Initial URL
http://www.six-degrees.com/six-degrees.html

Initial Description
hope you enjoy - thanks to Tom Duff (for implementation in C), Dean Edwards, and Nicholas Zakas for the idea

Initial Title
forEach function - fastest loop iteration

Initial Tags
array

Initial Language
JavaScript