JavaScript代码改进

时间:2009-12-02 02:18:56

标签: javascript asynchronous performance

我不是一个庞大的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);
})();

干杯!

7 个答案:

答案 0 :(得分:7)

写作方式的紧凑性和性能无关。但要以更紧凑,可重复使用的方式编写它:

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);

答案 1 :(得分:1)

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

可以成为:

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

这是我能看到的唯一改进,除非你愿意选择非标准的javascript,而不是创建元素,而是将实际的html元素转换为字符串,然后将其附加到文档.innerHTML

答案 2 :(得分:1)

var child1 = document.documentElement.firstChild;
child1.appendChild(jq);
child1.appendChild(cm);
child1.appendChild(an);
child1.appendChild(ga);

答案 3 :(得分:0)

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

答案 4 :(得分:0)

我确信这会因为“臃肿”而被贬低,但只是分享我将如何做到这一点:

首先,我将定义一个高度可扩展的功能:

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);

答案 5 :(得分:0)

好的,这是我对此的看法。不确定它现在节省了多少,但是如果你在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);
})();

答案 6 :(得分:0)

这是一种方法。希望这样可以直接添加或删除脚本(它们需要或不需要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();