Permalink
Browse files

Use a class for the cache entry objects

We make a LOT of these things in production environments.  V8 prefers
for hot-path objects to be of a consistent shape, and using a ctor
class makes this module go a bit faster.
  • Loading branch information...
1 parent a591fd7 commit 5c9c93960aa36f41559b53b39a4381d9825f2f4a @isaacs committed Sep 17, 2012
Showing with 12 additions and 7 deletions.
  1. +12 −7 lib/lru-cache.js
View
@@ -125,13 +125,9 @@ function LRUCache (options) {
return true
}
- var hit = {
- key:key,
- value:value,
- lu:mru++,
- length:lengthCalculator(value),
- now: (maxAge) ? Date.now() : 0
- }
+ var len = lengthCalculator(value)
+ var age = maxAge ? Date.now() : 0
+ var hit = new Entry(key, value, mru++, len, age)
// oversized objects fall out of cache automatically.
if (hit.length > max) {
@@ -183,4 +179,13 @@ function LRUCache (options) {
}
}
+// classy, since V8 prefers predictable objects.
+function Entry (key, value, mru, len, age) {
+ this.key = key
+ this.value = value
+ this.lu = mru
+ this.length = len
+ this.now = age
+}
+
})()

0 comments on commit 5c9c939

Please sign in to comment.