v8/test/mjsunit/es9/object-spread-basic.js
Benedikt Meurer 4995c85f28 [runtime] Optimize general object spread.
This adds a new %_CopyDataProperties intrinsic, that reuses most of the
existing machinery that we already have in place for Object.assign() and
computed property names in object literals. This speeds up the general
case for object spread (where the spread is not the first item in an
object literal) and brings it on par with Object.assign() at least - in
most cases it's significantly faster than Object.assign().

In the test case [1] referenced from the bug, the performance goes from

  objectSpreadLast: 3624 ms.
  objectAssignLast: 1938 ms.

to

  objectSpreadLast: 646 ms.
  objectAssignLast: 1944 ms.

which corresponds to a **5-6x performance boost**, making object spread
faster than Object.assign() in general.

Drive-by-fix: This refactors the Object.assign() fast-path in a way that
it can be reused appropriately for object spread, and adds another new
builtin SetDataProperties, which does the core of the Object.assign()
work. We can teach TurboFan to inline Object.assign() based on the new
SetDataProperties builtin at some later point to further optimize
Object.assign().

[1]: https://gist.github.com/bmeurer/0dae4a6b0e23f43d5a22d7c91476b6c0

Bug: v8:9167
Change-Id: I57bea7a8781c4a1e8ff3d394873c3cd4c5d73834
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1587376
Reviewed-by: Sathya Gunasekaran <gsathya@chromium.org>
Commit-Queue: Sathya Gunasekaran <gsathya@chromium.org>
Auto-Submit: Benedikt Meurer <bmeurer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#61100}
2019-04-29 18:47:19 +00:00

156 lines
3.9 KiB
JavaScript

// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var x = {a: 1};
var y = { ...x};
assertEquals(x, y);
assertEquals({}, y = { ...{} } );
assertEquals({}, y = { ...undefined });
assertEquals({}, y = { ...null });
assertEquals({}, y = { ...1 });
assertEquals({0: 'f', 1: 'o', 2: 'o'}, y = { ...'foo' });
assertEquals({0: 0, 1: 1}, y = { ...[0, 1] });
assertEquals({}, { ...new Proxy({}, {}) });
assertEquals({a: 2}, y = { ...x, a: 2 });
assertEquals({a: 1, b: 1}, y = { ...x, b: 1 });
assertEquals({a: 1}, y = { a: 2, ...x });
assertEquals({a: 1, b: 1}, y = { a:2, ...x, b: 1 });
assertEquals({a: 3}, y = { a: 2, ...x, a: 3 });
var z = { b: 1}
assertEquals({a: 1, b: 1}, y = { ...x, ...z });
assertEquals({a: 1, b: 1}, y = { a: 2, ...x, ...z });
assertEquals({a: 1, b: 1}, y = { b: 2, ...z, ...x });
assertEquals({a: 1, b: 1}, y = { a: 1, ...x, b: 2, ...z });
assertEquals({a: 1, b: 2}, y = { a: 1, ...x, ...z, b: 2 });
assertEquals({a: 2, b: 2}, y = { ...x, ...z, a:2, b: 2 });
var x = {}
Object.defineProperty(x, 'a', {
enumerable: false,
configurable: false,
writable: false,
value: 1
});
assertEquals({}, { ...x });
var x = {}
Object.defineProperty(x, 'a', {
enumerable: true,
configurable: false,
writable: false,
value: 1
});
var y = { ...x };
var prop = Object.getOwnPropertyDescriptor(y, 'a');
assertEquals(prop.value, 1);
assertTrue(prop.enumerable);
assertTrue(prop.configurable);
assertTrue(prop.writable);
var x = { __proto__: z }
assertEquals({}, { ...x });
var x = {
get a() { return 1; },
set a(_) { assertUnreachable("setter called"); },
};
assertEquals({ a: 1 }, y = { ...x });
var x = {
method() { return 1; },
};
assertEquals(x, y = { ...x });
var x = {
*gen() { return {value: 1, done: true} ; },
};
assertEquals(x, y = { ...x });
var x = {
get a() { throw new Error(); },
};
assertThrows(() => { y = { ...x } });
var p = new Proxy({}, {
ownKeys() { throw new Error(); }
});
assertThrows(() => { y = { ...p } });
var p = new Proxy({}, {
ownKeys() { [1]; },
get() { throw new Error(); }
});
assertThrows(() => { y = { ...p } });
var p = new Proxy({}, {
ownKeys() { [1]; },
getOwnPropertyDescriptor() { throw new Error(); }
});
assertThrows(() => { y = { ...p } });
var p = new Proxy(z, {
ownKeys() { return Object.keys(z); },
get(_, prop) { return z[prop]; },
getOwnPropertyDescriptor(_, prop) {
return Object.getOwnPropertyDescriptor(z, prop);
},
});
assertEquals(z, y = { ...p });
var x = { a:1 };
assertEquals(x, y = { set a(_) { throw new Error(); }, ...x });
var prop = Object.getOwnPropertyDescriptor(y, 'a');
assertEquals(prop.value, 1);
assertFalse("set" in prop);
assertTrue(prop.enumerable);
assertTrue(prop.configurable);
assertTrue(prop.writable);
var x = { a:2 };
assertEquals(x, y = { get a() { throw new Error(); }, ...x });
var prop = Object.getOwnPropertyDescriptor(y, 'a');
assertEquals(prop.value, 2);
assertFalse("get" in prop);
assertTrue(prop.enumerable);
assertTrue(prop.configurable);
assertTrue(prop.writable);
var x = { a:3 };
assertEquals(x, y = {
get a() {
throw new Error();
},
set a(_) {
throw new Error();
},
...x
});
var prop = Object.getOwnPropertyDescriptor(y, 'a');
assertEquals(prop.value, 3);
assertFalse("get" in prop);
assertFalse("set" in prop);
assertTrue(prop.enumerable);
assertTrue(prop.configurable);
assertTrue(prop.writable);
var x = Object.seal({ a:4 });
assertEquals(x, y = { ...x });
var prop = Object.getOwnPropertyDescriptor(y, 'a');
assertEquals(prop.value, 4);
assertTrue(prop.enumerable);
assertTrue(prop.configurable);
assertTrue(prop.writable);
var x = Object.freeze({ a:5 });
assertEquals(x, y = { ...x });
var prop = Object.getOwnPropertyDescriptor(y, 'a');
assertEquals(prop.value, 5);
assertTrue(prop.enumerable);
assertTrue(prop.configurable);
assertTrue(prop.writable);