zoukankan      html  css  js  c++  java
  • [读书笔记]高性能JS-编程实践

    This part is some example to practice.

    1 avoid double evaluation

    less use of eval,new Function,settimeout,setinterval.

    string in these method are executable js code.when use these method,js will execute code twice ,first is normal js,second is js code in string ,such as var a=eval('1+2');first Asignment,second run code in string '1+2';

    in normal,don't use new Function. try to use function instead of string both in serttimeout and setinterval;

    2 use literal;

    var obj={

      'a':1,

      "b":2

    }

    var arr=[1,2,3,4]

    these two are good and run fast than something as 

    var obj={};

    obj.a=1;

    obj.b=2;

    3 no repeated job

    normally, we addevent use the function as

    function addEvent(obj,type,handler){
        if(obj.addEventListener){
            obj.addEventListener(type,handler,false)
        }else{
            obj.attachEvent('on'+type,handler);
        }
    }
    function removeEvent(obj,type,handler){
        if(obj.removeEventListener){
            obj.removeEventListener(type,handler,false)
        }else{
            obj.detachEvent('on'+type,handler);
        }
    }
    when called ,function will do repeated job for check which browser we using. actually ,one test is enough.

    one way to avoid is lazy load.

    function addEvent(obj,type,handler){
    if(obj.addEventListener){
    addEvent= function (obj,type,handler) {
    obj.addEventListener(type,handler,false)
    }
    }else{
    addEvent= function (obj,type,handler) {
    obj.attachEvent('on'+type,handler);
    }
    }
    addEvent(obj,type,handler)
    }

    call this method once,and new addEvent will override the past one.so next time called,it save lots of time.

    and another way is conditional load.

    var addevent=document.body.addEventListener?
    function (obj,type,handler) {
    obj.addEventListener(type,handler,false)
    }: function (obj,type,handler){
    obj.attachEvent('on'+type,handler);
    }
    cool way.






  • 相关阅读:
    MyEclipse中代码提醒功能
    oracle12c创建用户等问题
    java中的构造块、静态块等说明
    jquery中的get和post、ajax有关返回值的问题描述
    最大半连通子图 BZOJ 1093
    最小生成树计数 BZOJ 1016
    水平可见直线 BZOJ 1007
    分金币 BZOJ 3293
    游走 BZOJ 3143
    糖果 BZOJ 2330
  • 原文地址:https://www.cnblogs.com/wz0107/p/4957416.html
Copyright © 2011-2022 走看看