ES6 Proxy 性能之我见

本文翻译自https://thecodebarbarian.com/thoughts-on-es6-proxies-performance

Proxy是ES6的一个强力功能,它通过为 get/set一个属性 设置"陷阱"(函数处理器)让我们可以拦截对于对象属性的操作。比如:

const obj = {};
const proxy = new Proxy(obj, {
get: () => {
console.log('hi');
}
}); obj.a; // "hi"

Proxy被称赞为现在已经被废弃的Object.observe()属性的取代者

然而不幸的是,Proxy有一个致命缺陷:性能。

更打击人的是,Object.observe()就是因为性能被废弃的,而以我(原作者)对V8的理解,对于JIT(Just in Time,准时制)来说,Object.observe()比Proxy容易优化多了。

Proxy到底有多慢?

我(原作者)在node v6.9.0中用benchmark简单试了一下:

var Benchmark = require('benchmark');

var suite = new Benchmark.Suite;

var obj = {};

var _obj = {};
var proxy = new Proxy(_obj, {
set: (obj, prop, value) => { _obj[prop] = value; }
}); var defineProp = {};
Object.defineProperty(defineProp, 'prop', {
configurable: false,
set: v => defineProp._v = v
}); // 译者注: vanilla js 指的就是原生js
suite.
add('vanilla', function() {
obj.prop = 5;
}).
add('proxy', function() {
proxy.prop = 5;
}).
add('defineProperty', function() {
defineProp.prop = 5;
}).
on('cycle', function(event) {
console.log(String(event.target));
}).
on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
}).
run();

结果如下:

$ node proxy.js
vanilla x 74,288,023 ops/sec ±0.78% (86 runs sampled)
proxy x 3,625,152 ops/sec ±2.51% (86 runs sampled)
defineProperty x 74,815,513 ops/sec ±0.80% (85 runs sampled)
Fastest is defineProperty,vanilla
$

从这个简单的benchmark中我们可以看到,Proxy的set 比直接赋值和defineProperty慢非常多(译者注:ops/sec,每秒进行的操作数,越大越快)。

为防大家好奇,我(原作者)又在node 4.2.1测试了一下Object.observe()

$ node proxy.js
vanilla x 78,615,272 ops/sec ±1.55% (84 runs sampled)
defineProperty x 79,882,188 ops/sec ±1.31% (85 runs sampled)
Object.observe() x 5,234,672 ops/sec ±0.86% (89 runs sampled)
Fastest is defineProperty,vanilla

有些文章可能让你觉得只要Proxy不用get/set而是只设置getOwnPropertyDescriptor()的话,就比其他的快,于是我(原作者)又试了试:

var _obj = {};
var propertyDescriptor = {
configurable: true,
set: v => { _obj.prop = v; }
};
var proxy = new Proxy(_obj, {
getOwnPropertyDescriptor: (target, prop) => propertyDescriptor
});

不幸的是,反而更慢了:

$ node proxy.js
vanilla x 73,695,484 ops/sec ±1.04% (88 runs sampled)
proxy x 2,026,006 ops/sec ±0.74% (90 runs sampled)
defineProperty x 74,137,733 ops/sec ±1.25% (88 runs sampled)
Fastest is defineProperty,vanilla
$

用Proxy包裹一个函数并调用同样比原生的包裹函数并调用慢非常多:

var Benchmark = require('benchmark');

var suite = new Benchmark.Suite;

var fn = () => 5;
var proxy = new Proxy(function() {}, {
apply: (target, context, args) => fn.apply(context, args)
}); var wrap = () => fn(); // add tests
suite.
add('vanilla', function() {
fn();
}).
add('proxy', function() {
proxy();
}).
add('wrap', function() {
wrap();
}).
on('cycle', function(event) {
console.log(String(event.target));
}).
on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
}).
run();
$ node proxy2.js
vanilla x 78,426,813 ops/sec ±0.93% (88 runs sampled)
proxy x 5,244,789 ops/sec ±2.17% (87 runs sampled)
wrap x 75,350,773 ops/sec ±0.85% (85 runs sampled)
Fastest is vanilla

无用的提升Proxy性能的方法

目前最有影响力的提升Proxy性能的方法是让被修改的属性的configurable设为false:

var _obj = {};
Object.defineProperty(_obj, 'prop', { configurable: false });
var propertyDescriptor = {
configurable: false,
enumerable: true,
set: v => { _obj.prop = v; }
};
var proxy = new Proxy(_obj, {
getOwnPropertyDescriptor: (target, prop) => propertyDescriptor
});

(译者注:这段代码有些问题,enumerableconfigurable为false时是无效的)

$ node proxy.js
vanilla x 74,622,163 ops/sec ±0.95% (85 runs sampled)
proxy x 4,649,544 ops/sec ±0.47% (85 runs sampled)
defineProperty x 77,048,878 ops/sec ±0.60% (88 runs sampled)
Fastest is defineProperty
$

要是这样写set/get,还不如直接用 Object.defineProperty()

这样写的话,你就不得不设置每个你要在Proxy中用到的属性不可配置(not configurable)。

不然的话,V8就会报错:

var _obj = {};
Object.freeze(_obj);
var propertyDescriptor = {
configurable: false,
enumerable: true,
set: v => { _obj.prop = v; }
};
var proxy = new Proxy(_obj, {
getOwnPropertyDescriptor: (target, prop) => propertyDescriptor
}); // Throws:
// "TypeError: 'getOwnPropertyDescriptor' on proxy: trap returned
// descriptor for property 'prop' that is incompatible with the
// existing property in the proxy target"
// 拦截'prop'属性返回的descriptor和target(原对象)已经存在的属性不匹配
proxy.prop = 5;

Proxy 也要不行了么?

Proxy比 Object.defineProperty()有不少优点:

  • Proxy 可以嵌套,而Object.defineProperty()getter/setter就不能嵌套,这样你就不需要知道提前知道你要拦截的所有属性
  • 可以拦截数组变化

但它性能太差了。

性能有多大影响呢?

以Promise和回调为例:

var Benchmark = require('benchmark');

var suite = new Benchmark.Suite;

var handleCb = cb => cb(null);

// add tests
suite.
add('new function', function() {
handleCb(function(error, res) {});
}).
add('new promise', function() {
return new Promise((resolve, reject) => {});
}).
add('promise resolve', function() {
Promise.resolve().then(() => {});
}).
on('cycle', function(event) {
console.log(String(event.target));
}).
on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
}).
run();
$ node promise.js
new function x 26,282,805 ops/sec ±0.74% (90 runs sampled)
new promise x 1,953,037 ops/sec ±1.02% (86 runs sampled)
promise resolve x 194,173 ops/sec ±13.80% (61 runs sampled)
Fastest is new function
$

Promise也慢了非常多,

但是 bluebird声称为Promise提供"非常好的性能",测试一下:

$ node promise.js
new function x 26,986,342 ops/sec ±0.48% (89 runs sampled)
new promise x 11,157,758 ops/sec ±1.05% (87 runs sampled)
promise resolve x 671,079 ops/sec ±27.01% (18 runs sampled)
Fastest is new function

虽然快了很多,但仍然比回调慢不少。

所以我们要因此放弃Promise么?

并不是这样的,很多公司仍然选择了使用Promise。我(原作者)虽然不是很确定,但是Uber好像就在使用Promise。

结论

Proxy很慢,但是在你因其性能而放弃它之前,记得同样性能很差的Promise在最近几年中被快速采用。

如果你想使用代理,很可能你不会感觉到性能的影响,除非你发现自己为了性能的原因改变了Promise库(或者完全避开了它们)。

更新

2019.01, 在node v11.3.0中: Promise已经变得足够好, Proxy还是那样

vanilla x 833,244,386 ops/sec ±0.76% (89 runs sampled)
proxy x 28,590,800 ops/sec ±0.72% (88 runs sampled)
wrap x 824,349,552 ops/sec ±0.87% (86 runs sampled)
Fastest is vanilla,wrap
new function x 834,121,566 ops/sec ±0.82% (89 runs sampled)
new promise x 819,789,350 ops/sec ±0.76% (87 runs sampled)
promise resolve x 1,212,009 ops/sec ±40.98% (30 runs sampled)
Fastest is new function

ES6 Proxy 性能之我见的更多相关文章

  1. Vue3.0 响应式数据原理:ES6 Proxy

    Vue3.0 开始用 Proxy 代替 Object.defineProperty了,这篇文章结合实例教你如何使用Proxy 本篇文章同时收录[前端知识点]中,链接直达 阅读本文您将收获 JavaSc ...

  2. ES6 Proxy和Reflect(下)

    construct() construct方法用于拦截new命令. var handler = { construct (target, args) { return new target(...ar ...

  3. ES6 Proxy和Reflect (上)

    Proxy概述 Proxy用于修改某些操作的默认行为,等同于在语言层面做出修改,所以属于一种"元编程"(meta programming),即对编程语言进行编程. Proxy可以理 ...

  4. es6 proxy代理

    es6 新增构造函数 Proxy Proxy 构造函数,可以使用new 去创建,可以往里面插入两个参数,都是对象 let target = {} let handler = {} let proxy ...

  5. es6 Proxy

    proxy在语言层面去操作一个对象 var user={}; user.fname='Bob'; user.lname="Wood"; user.fullName= functio ...

  6. ES6 Proxy的应用场景

    一.相关API Proxy Reflect 二.Proxy应用场景 1.数据校验 表单提交的时候做数据校验,例如年龄是不是满足条件,数据类型是不是满足要求等等,这场场景非常适合使用Proxy. 下面展 ...

  7. es6 Proxy对象详解

    Proxy用于修改某些操作的默认行为,也可以理解为在目标对象之前架设一层拦截,外部所有的访问都必须先通过这层拦截,因此提供了一种机制,可以对外部的访问进行过滤和修改.这个词的原理为代理,在这里可以表示 ...

  8. es6 proxy浅析

    Proxy 使用proxy,你可以把老虎伪装成猫的外表,这有几个例子,希望能让你感受到proxy的威力. proxy 用来定义自定义的基本操作行为,比如查找.赋值.枚举性.函数调用等. proxy接受 ...

  9. 新的知识点来了-ES6 Proxy代理 和 去银行存款有什么关系?

    ES给开发者提供了一个新特性:Proxy,就是代理的意思.也就是我们这一节要介绍的知识点. 以前,ATM还没有那么流行的时候(暴露年纪),我们去银行存款或者取款的时候,需要在柜台前排队,等柜台工作人员 ...

随机推荐

  1. 关于c# winform使用FidderCore.dll 遇到的一些问题,请求支援

    小弟最近再研究winform用fidder抓取包的过程.开始都很顺利,并且成功开启了代理网络.同时手机也设置代理,并且手机可以上网,而且电脑也能抓到手机的请求. 但是遇到两个问题. 1.,这里的关闭代 ...

  2. windows控制台(console)乱码

    在cmd中输入 CHCP 65001,实操有效.记录一下 永久设置,代码如下: Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Cons ...

  3. js数组操作find查找特定值结合es6特性

    js数组操作find查找特定值结合es6特性

  4. LeetCode -- 1038. Binary Search Tree to Greater Sum Tree

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  5. 单调队列 && 单调栈

    单调队列 && 单调栈 单调队列 维护某个滑动区间的min or max,可用于dp的优化 以维护min为例,采用STL双端队列实现 每次加入元素x前 先检查队首元素==滑动后要删除的 ...

  6. 【Shell编程】Shell程序设计

    1.Shell简介   作为Linux灵感来源的Unix系统最初是没有图形化界面的,所有的任务都是通过命令行来实现的.因此,Unix的命令行系统得到了很大的发展,逐步成为一个功能强大的系统.   Sh ...

  7. DOM学习之充实文档内容

    HTML代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <me ...

  8. 51nod1185 威佐夫游戏 V2【博弈论】

    有2堆石子.A B两个人轮流拿,A先拿.每次可以从一堆中取任意个或从2堆中取相同数量的石子,但不可不取.拿到最后1颗石子的人获胜.假设A B都非常聪明,拿石子的过程中不会出现失误.给出2堆石子的数量, ...

  9. Mybatis配置之别名配置元素详述

    这里我们贴出之前的UserDao对应的mapper文件,如下所示 从这个配置文件中,我们可以看到<select>.<insert>和<update>三个标签元素的r ...

  10. 邓_ SVN·最新使用教程总结

    SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什么? ...