JavaScript代码改进

JavaScript代码改进,javascript,asynchronous,performance,Javascript,Asynchronous,Performance,我不是一个巨大的JavaScript性能大师。我只是想知道,我可以让下面的代码更加紧凑吗?不是包装或压缩,而是书写的方式 (function() { var jq = document.createElement('script'); var an = document.createElement('script'); var cm = document.createElement('script'); var ga = document.createElemen

我不是一个巨大的JavaScript性能大师。我只是想知道,我可以让下面的代码更加紧凑吗?不是包装或压缩,而是书写的方式

(function() {
    var jq = document.createElement('script');
    var an = document.createElement('script');
    var cm = document.createElement('script');
    var ga = document.createElement('script');
    var domain = 'http://example.com/';

    jq.src = domain + 'jquery.1.3.2.js';
    an.src = domain + 'jquery.alphanumeric.js';
    cm.src = domain + 'common.js';
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');

    document.documentElement.firstChild.appendChild(jq);
    document.documentElement.firstChild.appendChild(cm);
    document.documentElement.firstChild.appendChild(an);
    document.documentElement.firstChild.appendChild(ga);
})();
干杯,伙计们

'https:' == document.location.protocol ? 'https://ssl' : 'http://www'
可以成为:

'http' + 'https:'==document.location.protocol ? 's://ssl' : '://www'

这是我能看到的唯一改进,除非您愿意使用非标准javascript,而不是创建元素,而是将实际的html元素添加到字符串中,然后将其附加到文档中。innerHTML编写方式的紧凑性与性能无关。但要以更紧凑、可重用的方式编写它:

function appendScript(url, async) {
    var el = document.createElement('script'),
        root = document.documentElement;
    el.async = async;
    el.src = url;
    // avoid an IE6 bug by using insertBefore (http://bugs.jquery.com/ticket/2709)
    root.insertBefore(el, root.firstChild);
}


appendScript('http://example.com/js/jquery.1.3.2.js', false);
appendScript('http://example.com/js/jquery.alphanumeric.js', false);
appendScript('http://example.com/js/common.js', false);
appendScript(('https:' == document.location.protocol ? '//ssl' : '//www') + '.google-analytics.com/ga.js'), true);

您可以创建一个addScriptElement函数,以减少重复性。

我确信这将被否决为“膨胀”,但只是分享我将如何做到这一点:

首先,我将定义这样一个高度可扩展的函数:

function addElements(objlist) {
    // One or many
    objlist = [].concat(objlist);

    while(objlist.length > 0) {
        var current = objlist.pop();

        var node = document.createElement(current.element || 'div');
        for(attr in current.attributes)
            node.setAttribute(attr, current.attributes[attr]);

        if(current.parent)
            current.parent.appandChild(node);
    }
}
然后,要使用它:

addElements([
    {
        parent: document.documentElement.firstChild,
        element: 'script',
        attributes: {
            src: 'http://example.com/jquery.1.3.2.js'
        }
    },
    {
        parent: document.documentElement.firstChild,
        element: 'script',
        attributes: {
            src: 'http://example.com/jquery.alphanumeric.js'
        }
    },
    {
        parent: document.documentElement.firstChild, 
        element: 'script',
        attributes: {
            src: 'http://example.com/common.js'
        }
    },
    {
        parent: document.documentElement.firstChild, 
        element: 'script',
        attributes: {
            src: ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js',
            async: true
        }
    }
]);
这就是我所说的“幂函数”。它的可读性很强,尽管有重复,但表达力很强

您甚至可以自动创建对象:

var elements = [
    'jquery.1.3.2.js',
    'jquery.alphanumeric.js',
    'common.js',
    ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'
];

for(var i=0; i<4; ++i) {
    elements[i] = {
        element: 'script',
        parent: document.documentElement.firstChild,
        attributes: {
            src: 'http://example.com/' + elements[i]
        }
    };
}

elements[3].attributes.async = true;

addElements(elements);

好的,这是我的照片。现在还不确定它能省下多少钱,但如果你在example.com上拥有更多的资产,它会加快速度

(function(){
    var scripts    = ['jquery.1.3.2', 'jquery.alphanumeric', 'common'],
        head       = document.documentElement.firstChild,
        domain     = 'http://example.com/',
        add_script = function(url, async){
            var script = document.createElement('script');
            script.src = url;
            if(async === true) script.setAttribute('async', 'true');
            head.appendChild(script);
        };

    for(script in scripts) add_script( domain + scripts[script] + '.js' );

    add_script( ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js', true);
})();

这里有一种方法。希望这样可以直接添加或删除不需要或不需要async属性的脚本:

({
    DOMAIN : 'http://example.com/',
    SCRIPTS : [ {file:'jquery.1.3.2.js'},
            {file:'jquery.alphanumeric.js'},
            {file:'common.js'},
            {file: ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'
                , async: 'true'} ],
    init: function() {
        for (var i in this.SCRIPTS) {
            var script = this.SCRIPTS[i];
            var sc = document.createElement('script');
            sc.src = (script.file.match(/^http/gi)) ? sc.src = script.file : sc.src = this.DOMAIN + script.file;
            if (typeof script.async !== 'undefined') {
                sc.setAttribute('async', script.async);
            }
            document.documentElement.firstChild.appendChild(sc);
        }

    }
}).init();

有趣的我只是想知道是否有一种方法可以组合var或所有dom指令。我想不是这样的:+1但是挑剔的话,为域使用一个变量可能是值得的。你肯定会希望域成为一个变量-这个重构增加了灵活性。肯定吗?正如宴会上所说,这可能是值得的。在我看来,这是一个简单的问题,遇到了一个简单的解决方案。将域设为变量会增加复杂性(尽管很小),并降低性能,但实际上没有任何好处—节省几字节的带宽?我会绝对放弃—很好,但如果您需要在开发服务器或本地服务器上使用它,则需要重写。当然,你可以搜索和替换,但是把它作为一个变量让其他开发人员可以很容易地修改。好的一点,如果这是担心脚本URL可能只是相对的。我不打算否决,但是为什么要为一些只需要几行就可以完成的事情费那么大的劲呢?为了“权力”。我喜欢泛化和编写能够“完成一切”的函数。在这种情况下,这似乎是一个不好的例子,但它让我想写并分享这样一个方法。不过,很高兴与大家分享。在你的第一行中有你的免责声明,你不会得到任何反对票。当然不是我说的:P
({
    DOMAIN : 'http://example.com/',
    SCRIPTS : [ {file:'jquery.1.3.2.js'},
            {file:'jquery.alphanumeric.js'},
            {file:'common.js'},
            {file: ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'
                , async: 'true'} ],
    init: function() {
        for (var i in this.SCRIPTS) {
            var script = this.SCRIPTS[i];
            var sc = document.createElement('script');
            sc.src = (script.file.match(/^http/gi)) ? sc.src = script.file : sc.src = this.DOMAIN + script.file;
            if (typeof script.async !== 'undefined') {
                sc.setAttribute('async', script.async);
            }
            document.documentElement.firstChild.appendChild(sc);
        }

    }
}).init();