javascript - underscore源码

查看:179
本文介绍了javascript - underscore源码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

var optimizeCb = function (func, context, argCount) {
        if (context === void 0) return func
        switch (argCount == null ? 3 : argCount) {
        case 1: 
            return function (value) {
                return func.call(context, value)
            }
        case 2:
            return function (value, other) {
                return func.call(context, value, other)
            }
        case 3:
            return function (value, index, collection) {
                return func.call(context, value, index, collection)
            }
        case 4:
            return function (accumulator, value, index, collection) {
                return func.call(context, accumulator, value, index, collection)
            }
        }
        return function () {
            return func.apply(context, arguments)
        }
}

求教:上面是underscore的一段源码,让我比较困惑的是switch的作用,我觉得完全不需要switch啊,直接如果判断context==null,如果不是直接返回

return function () {
            return func.apply(context, arguments)
        }

上面这个不就可以吗?求各位大神指教啊?它的注释中说是

// Internal function that returns an efficient (for current engines) version
    // of the passed-in callback, to be repeatedly applied in other Underscore
    // functions.

请问是对于比较少参数的时候,用call这种方法会快不少吗?感觉有别的深意啊,毕竟我不觉得这个速度会差很多,可是实在想不到区别了

解决方案

没有什么深意,单纯就是因为.call.apply快很多。

这个速度的差距可能会让你感到吃惊:即使在比较新的浏览器里(Chrome 50+),.call也比.apply快上一倍。旧一点的JS引擎中.call甚至能比.apply快10倍以上。

不信在Console里试试:

function work(a, b, c) {}

var a = [1, 2, 3];

for (var j = 0; j < 5; j++) {
  console.time('apply');
  for (var i = 0; i < 1000000; i++) {
    work.apply(this, a);
  }
  console.timeEnd('apply');

  console.time('call');
  for (var i = 0; i < 1000000; i++) {
    work.call(this, 1, 2, 3);
  }
  console.timeEnd('call');
}

原因在与.apply在运行前要对作为参数的数组进行一系列检验和深拷贝,.call则没有这些步骤。具体可以参考ECMA 5.1 标准:
http://www.ecma-international...
http://www.ecma-international...

这篇关于javascript - underscore源码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆