]> git.parisson.com Git - pdf.js.git/commitdiff
Cache Cmd object to reduce number of created objects
authorJulian Viereck <julian.viereck@gmail.com>
Sun, 18 Dec 2011 19:39:10 +0000 (20:39 +0100)
committerJulian Viereck <julian.viereck@gmail.com>
Sun, 18 Dec 2011 19:39:10 +0000 (20:39 +0100)
src/obj.js
src/parser.js

index 2f7488a768b65ed8f4d3fc0e2d5137c357f1bdd5..cb29f4c3cf602b9d2a057cfbf01e1376b7faf350 100644 (file)
@@ -22,6 +22,17 @@ var Cmd = (function CmdClosure() {
   Cmd.prototype = {
   };
 
+
+  var cmdCache = {};
+
+  Cmd.get = function(cmd) {
+    if (cmdCache[cmd]) {
+      return cmdCache[cmd];
+    } else {
+      return cmdCache[cmd] = new Cmd(cmd);
+    }
+  }
+
   return Cmd;
 })();
 
index 695438379eda46b9ed65276b8bf4b6a3e8440147..e50b12b9b2dcfbfac85be4856ef6883ad5748e6e 100644 (file)
@@ -157,7 +157,7 @@ var Parser = (function ParserClosure() {
       imageStream = this.filter(imageStream, dict, length);
       imageStream.parameters = dict;
 
-      this.buf2 = new Cmd('EI');
+      this.buf2 = Cmd.get('EI');
       this.shift();
 
       return imageStream;
@@ -496,14 +496,14 @@ var Lexer = (function LexerClosure() {
         // array punctuation
         case '[':
         case ']':
-          return new Cmd(ch);
+          return Cmd.get(ch);
         // hex string or dict punctuation
         case '<':
           ch = stream.lookChar();
           if (ch == '<') {
             // dict punctuation
             stream.skip();
-            return new Cmd('<<');
+            return Cmd.get('<<');
           }
           return this.getHexString(ch);
         // dict punctuation
@@ -511,11 +511,11 @@ var Lexer = (function LexerClosure() {
           ch = stream.lookChar();
           if (ch == '>') {
             stream.skip();
-            return new Cmd('>>');
+            return Cmd.get('>>');
           }
         case '{':
         case '}':
-          return new Cmd(ch);
+          return Cmd.get(ch);
         // fall through
         case ')':
           error('Illegal character: ' + ch);
@@ -538,7 +538,7 @@ var Lexer = (function LexerClosure() {
         return false;
       if (str == 'null')
         return null;
-      return new Cmd(str);
+      return Cmd.get(str);
     },
     skipToNextLine: function lexerSkipToNextLine() {
       var stream = this.stream;