Skip to content

Commit 86c4a61

Browse files
committed
fix v8 optimisation issues
1 parent 422c1e4 commit 86c4a61

4 files changed

Lines changed: 37 additions & 29 deletions

File tree

src/collision/Grid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ var Common = require('../core/Common');
214214
* @return {string} bucket id
215215
*/
216216
var _getBucketId = function(column, row) {
217-
return column + ',' + row;
217+
return 'C' + column + 'R' + row;
218218
};
219219

220220
/**

src/collision/Pair.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ var Contact = require('./Contact');
117117
*/
118118
Pair.id = function(bodyA, bodyB) {
119119
if (bodyA.id < bodyB.id) {
120-
return bodyA.id + '_' + bodyB.id;
120+
return 'A' + bodyA.id + 'B' + bodyB.id;
121121
} else {
122-
return bodyB.id + '_' + bodyA.id;
122+
return 'A' + bodyB.id + 'B' + bodyA.id;
123123
}
124124
};
125125

src/collision/SAT.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ var Vector = require('../geometry/Vector');
2828
overlapBA,
2929
minOverlap,
3030
collision,
31-
prevCol = previousCollision,
3231
canReusePrevCol = false;
3332

34-
if (prevCol) {
33+
if (previousCollision) {
3534
// estimate total motion
3635
var parentA = bodyA.parent,
3736
parentB = bodyB.parent,
@@ -40,20 +39,20 @@ var Vector = require('../geometry/Vector');
4039

4140
// we may be able to (partially) reuse collision result
4241
// but only safe if collision was resting
43-
canReusePrevCol = prevCol && prevCol.collided && motion < 0.2;
42+
canReusePrevCol = previousCollision && previousCollision.collided && motion < 0.2;
4443

4544
// reuse collision object
46-
collision = prevCol;
45+
collision = previousCollision;
4746
} else {
4847
collision = { collided: false, bodyA: bodyA, bodyB: bodyB };
4948
}
5049

51-
if (prevCol && canReusePrevCol) {
50+
if (previousCollision && canReusePrevCol) {
5251
// if we can reuse the collision result
5352
// we only need to test the previously found axis
5453
var axisBodyA = collision.axisBody,
5554
axisBodyB = axisBodyA === bodyA ? bodyB : bodyA,
56-
axes = [axisBodyA.axes[prevCol.axisNumber]];
55+
axes = [axisBodyA.axes[previousCollision.axisNumber]];
5756

5857
minOverlap = _overlapAxes(axisBodyA.vertices, axisBodyB.vertices, axes);
5958
collision.reused = true;
@@ -94,7 +93,6 @@ var Vector = require('../geometry/Vector');
9493
collision.bodyA = bodyA.id < bodyB.id ? bodyA : bodyB;
9594
collision.bodyB = bodyA.id < bodyB.id ? bodyB : bodyA;
9695
collision.collided = true;
97-
collision.normal = minOverlap.axis;
9896
collision.depth = minOverlap.overlap;
9997
collision.parentA = collision.bodyA.parent;
10098
collision.parentB = collision.bodyB.parent;
@@ -103,20 +101,27 @@ var Vector = require('../geometry/Vector');
103101
bodyB = collision.bodyB;
104102

105103
// ensure normal is facing away from bodyA
106-
if (Vector.dot(collision.normal, Vector.sub(bodyB.position, bodyA.position)) > 0)
107-
collision.normal = Vector.neg(collision.normal);
104+
if (Vector.dot(minOverlap.axis, Vector.sub(bodyB.position, bodyA.position)) < 0) {
105+
collision.normal = {
106+
x: minOverlap.axis.x,
107+
y: minOverlap.axis.y
108+
};
109+
} else {
110+
collision.normal = {
111+
x: -minOverlap.axis.x,
112+
y: -minOverlap.axis.y
113+
};
114+
}
108115

109116
collision.tangent = Vector.perp(collision.normal);
110117

111-
collision.penetration = {
112-
x: collision.normal.x * collision.depth,
113-
y: collision.normal.y * collision.depth
114-
};
118+
collision.penetration = collision.penetration || {};
119+
collision.penetration.x = collision.normal.x * collision.depth;
120+
collision.penetration.y = collision.normal.y * collision.depth;
115121

116122
// find support points, there is always either exactly one or two
117123
var verticesB = _findSupports(bodyA, bodyB, collision.normal),
118-
supports = collision.supports || [];
119-
supports.length = 0;
124+
supports = [];
120125

121126
// find the supports from bodyB that are inside bodyA
122127
if (Vertices.contains(bodyA.vertices, verticesB[0]))

src/core/Common.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@ module.exports = Common;
3333
deepClone = true;
3434
}
3535

36-
args = Array.prototype.slice.call(arguments, argsStart);
37-
38-
for (var i = 0; i < args.length; i++) {
39-
var source = args[i];
36+
for (var i = argsStart; i < arguments.length; i++) {
37+
var source = arguments[i];
4038

4139
if (source) {
4240
for (var prop in source) {
@@ -493,11 +491,10 @@ module.exports = Common;
493491
* @return {function} A new function that calls the passed functions in order.
494492
*/
495493
Common.chain = function() {
496-
var args = Array.prototype.slice.call(arguments),
497-
funcs = [];
494+
var funcs = [];
498495

499-
for (var i = 0; i < args.length; i += 1) {
500-
var func = args[i];
496+
for (var i = 0; i < arguments.length; i += 1) {
497+
var func = arguments[i];
501498

502499
if (func._chained) {
503500
// flatten already chained functions
@@ -508,10 +505,16 @@ module.exports = Common;
508505
}
509506

510507
var chain = function() {
511-
var lastResult;
508+
// https://github.com/GoogleChrome/devtools-docs/issues/53#issuecomment-51941358
509+
var lastResult,
510+
args = new Array(arguments.length);
511+
512+
for (var i = 0, l = arguments.length; i < l; i++) {
513+
args[i] = arguments[i];
514+
}
512515

513-
for (var i = 0; i < funcs.length; i += 1) {
514-
var result = funcs[i].apply(lastResult, arguments);
516+
for (i = 0; i < funcs.length; i += 1) {
517+
var result = funcs[i].apply(lastResult, args);
515518

516519
if (typeof result !== 'undefined') {
517520
lastResult = result;

0 commit comments

Comments
 (0)