`
hite
  • 浏览: 52041 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

James Padolsey :Partial loop “unrolling”

阅读更多

 

Ps:本人不甚解,这样做的意义。

Posted: 13 Nov 2009 01:31 AM PST

In Thomas Fuchs’ latest JavaScript performance presentation he talks about the speed gains that can be experienced by using “unrolled” loops.

A conventional loop:

for (var i = 0; i < 10; ++i) {
    doFoo(i);
}

The “unrolled” version of that loop:

var i = 0;
doFoo(i++); doFoo(i++); doFoo(i++); doFoo(i++); doFoo(i++);
doFoo(i++); doFoo(i++); doFoo(i++); doFoo(i++); doFoo(i++);

A partially unrolled version:

for (var i = 0; i < 10; ) {
    doFoo(i++);
    doFoo(i++);
    doFoo(i++);
    doFoo(i++);
    doFoo(i++);
}

Interestingly, speed gains can be experienced dependent on the loop size, albeit marginal at best. I thought about ways to build this into a clever forEach function and came up with something that ‘pre-compiles’ functions containing partially unrolled loops. Have a look:

var forEach = (function() {
 
    var fns = [],
        callers = "true",
        numberFn = 10,
        i = 1;
 
    for ( ; i <= numberFn; ++i ) {
        callers += "&&f(a[++i])!==false";
        fns[i] = new Function("a", "f", "l", "var i=0;while (i<l) {"+callers+"}");
    }
 
    return function( array, fn ) {
 
        var len = array.length,
            n = numberFn, i;
 
        while (i = n--) {
            if ( len % i === 0 ) {
                return fns[i](array, fn, len);
            }
        }
 
    };
 
})();

This function will run one of 10 ‘pre-compiled’ functions on the passed array, dependent on the highest factor of the array’s length. I’m only creating 10 different functions in this example, you could create more.

If you were to pass an array with a length of 14, then fns[7] would be used, since 7 is the highest available factor (the highest number below 10 that 14 can be divided by, to gain a whole number). fns[7] looks something like this:

function anonymous(a, f, l) {
    var i = 0;
    while (i < l) {
        true &&
            f(a[++i]) !== false &&
            f(a[++i]) !== false &&
            f(a[++i]) !== false &&
            f(a[++i]) !== false &&
            f(a[++i]) !== false &&
            f(a[++i]) !== false &&
            f(a[++i]) !== false;
    }
}

The !== false part is used to create the effect of loop-breaking. Notice that the success of this boolean expression is depended upon to continue the chain of expressions (a && b && c)

I’ve only tested it briefly, and to be honest, there doesn’t seem to be a notable benefit. In IE, I can see a bit of improvement over the conventional forEach implementation but only if I’m using arrays with 1000+ lengths. I think this would only be useful in situations where you absolutely have to squeeze every inch of potential performance out of your app. Anyway, it’s still pretty interesting, I wonder what other fancy things can be created by using pre-compiled functions.

 

 

分享到:
评论

相关推荐

    padolsey:产生padolsey模式

    帕多西程序生成padolsey模式es模块的用法import padolsey from 'padolsey'window . addEventListener ( 'click' , function ( ) { document . body . style . background = `url( ${ padolsey ( ) } )`} )

    jQuery插件Cookie操作jQuery.Cookie -源码.zip

    jQuery.Cookie是由James Padolsey编写的插件,它简化了JavaScript中的Cookie操作。这个插件允许我们轻松地读取、设置和删除Cookie,极大地提高了开发效率。其核心功能包括: 1. 设置Cookie:`$.cookie(name, value,...

    Fastest way to build an HTML string(拼装html字符串的最快方法)

    Fastest way to build an HTML stringPosted in ‘Code Snippets, JavaScript’ by James on May 29th, 2009 原文:http://james.padolsey.com/javascript/fastest-way-to-build-an-html-string/ 代码如下: var arr...

    Kwicks 基于jquery的图片展示横向手风琴效果.zip

    Kwicks是jQuery插件之一,由James Padolsey开发,旨在创建灵活且可定制的水平手风琴效果。其名称来源于“Quick”(快速)和“Kicks”(乐趣),意味着它能快速地为网页增添趣味性。不同于传统的垂直手风琴,Kwicks...

    jQuery基础

    书中提到的contributions是由Rebecca Murphey以及James Padolsey、Paul Irish等人所作的贡献,他们的工作可以在GitHub仓库中找到完整的贡献历史。这本书的使用并没有版权限制,可以自由地复制、分发、传输和重新混合...

    ie6中png透明解决方案 js

    其中,一种常见的解决方案是使用James Padolsey的JavaScript库,它通过检测浏览器版本并插入适当的CSS滤镜来解决IE6下的PNG透明问题。例如,可以使用以下代码: ```javascript function fixPNG() { if (window....

    超级酷和最实用的jQuery实例收集(20个)

    James Padolsey Color Switcher 点击右上角的颜色方块,可以换配色。 Visit Site Dragon Interactives jQuery Navigation 定制了jQuery的浏览菜单,把鼠标移到上面,渐变非常漂亮。 Visit Site FamSpams FaceBox ...

    使用js检测浏览器的实现代码

    另外,James Padolsey的条件注释检测也是一种巧妙的方法,它利用了IE特有的条件注释特性: ```javascript var ie = (function(){ var undef, v = 3, div = document.createElement('div'), all = div....

Global site tag (gtag.js) - Google Analytics