From 0d4a6f400086ee535fabcf1c73e10d634931ccee Mon Sep 17 00:00:00 2001 From: liabru Date: Fri, 28 Mar 2014 18:36:14 +0000 Subject: [PATCH] replaced "in" operator usage with access checks, it's a little faster --- src/collision/Detector.js | 4 ++-- src/collision/Pair.js | 7 ++++--- src/collision/Pairs.js | 8 ++++---- src/core/Events.js | 4 ++-- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/collision/Detector.js b/src/collision/Detector.js index b737992..e50a8eb 100644 --- a/src/collision/Detector.js +++ b/src/collision/Detector.js @@ -41,7 +41,7 @@ var Detector = {}; // find a previous collision we could reuse var pairId = Pair.id(bodyA, bodyB), - pair = pairId in pairsTable ? pairsTable[pairId] : null, + pair = pairsTable[pairId], previousCollision; if (pair && pair.isActive) { @@ -100,7 +100,7 @@ var Detector = {}; // find a previous collision we could reuse var pairId = Pair.id(bodyA, bodyB), - pair = pairId in pairsTable ? pairsTable[pairId] : null, + pair = pairsTable[pairId], previousCollision; if (pair && pair.isActive) { diff --git a/src/collision/Pair.js b/src/collision/Pair.js index 0e089be..12de206 100644 --- a/src/collision/Pair.js +++ b/src/collision/Pair.js @@ -56,10 +56,11 @@ var Pair = {}; if (collision.collided) { for (var i = 0; i < supports.length; i++) { var support = supports[i], - contactId = Contact.id(support); + contactId = Contact.id(support), + contact = contacts[contactId]; - if (contactId in contacts) { - activeContacts.push(contacts[contactId]); + if (contact) { + activeContacts.push(contact); } else { activeContacts.push(contacts[contactId] = Contact.create(support)); } diff --git a/src/collision/Pairs.js b/src/collision/Pairs.js index 65b818e..61999d6 100644 --- a/src/collision/Pairs.js +++ b/src/collision/Pairs.js @@ -55,11 +55,11 @@ var Pairs = {}; if (collision.collided) { pairId = Pair.id(collision.bodyA, collision.bodyB); activePairIds.push(pairId); - - if (pairId in pairsTable) { - // pair already exists (but may or may not be active) - pair = pairsTable[pairId]; + pair = pairsTable[pairId]; + + if (pair) { + // pair already exists (but may or may not be active) if (pair.isActive) { // pair exists and is active collisionActive.push(pair); diff --git a/src/core/Events.js b/src/core/Events.js index f7a4469..266ce2c 100644 --- a/src/core/Events.js +++ b/src/core/Events.js @@ -67,9 +67,9 @@ var Events = {}; for (var i = 0; i < names.length; i++) { name = names[i]; + callbacks = object.events[name]; - if (name in object.events) { - callbacks = object.events[name]; + if (callbacks) { eventClone = Common.clone(event, false); eventClone.name = name; eventClone.source = object;