Browse Source

Cache Cmd object to reduce number of created objects

Julian Viereck 14 years ago
parent
commit
156e20ca73
  1. 11
      src/obj.js
  2. 12
      src/parser.js

11
src/obj.js

@ -22,6 +22,17 @@ var Cmd = (function CmdClosure() {
Cmd.prototype = { Cmd.prototype = {
}; };
var cmdCache = {};
Cmd.get = function(cmd) {
if (cmdCache[cmd]) {
return cmdCache[cmd];
} else {
return cmdCache[cmd] = new Cmd(cmd);
}
}
return Cmd; return Cmd;
})(); })();

12
src/parser.js

@ -157,7 +157,7 @@ var Parser = (function ParserClosure() {
imageStream = this.filter(imageStream, dict, length); imageStream = this.filter(imageStream, dict, length);
imageStream.parameters = dict; imageStream.parameters = dict;
this.buf2 = new Cmd('EI'); this.buf2 = Cmd.get('EI');
this.shift(); this.shift();
return imageStream; return imageStream;
@ -496,14 +496,14 @@ var Lexer = (function LexerClosure() {
// array punctuation // array punctuation
case '[': case '[':
case ']': case ']':
return new Cmd(ch); return Cmd.get(ch);
// hex string or dict punctuation // hex string or dict punctuation
case '<': case '<':
ch = stream.lookChar(); ch = stream.lookChar();
if (ch == '<') { if (ch == '<') {
// dict punctuation // dict punctuation
stream.skip(); stream.skip();
return new Cmd('<<'); return Cmd.get('<<');
} }
return this.getHexString(ch); return this.getHexString(ch);
// dict punctuation // dict punctuation
@ -511,11 +511,11 @@ var Lexer = (function LexerClosure() {
ch = stream.lookChar(); ch = stream.lookChar();
if (ch == '>') { if (ch == '>') {
stream.skip(); stream.skip();
return new Cmd('>>'); return Cmd.get('>>');
} }
case '{': case '{':
case '}': case '}':
return new Cmd(ch); return Cmd.get(ch);
// fall through // fall through
case ')': case ')':
error('Illegal character: ' + ch); error('Illegal character: ' + ch);
@ -538,7 +538,7 @@ var Lexer = (function LexerClosure() {
return false; return false;
if (str == 'null') if (str == 'null')
return null; return null;
return new Cmd(str); return Cmd.get(str);
}, },
skipToNextLine: function lexerSkipToNextLine() { skipToNextLine: function lexerSkipToNextLine() {
var stream = this.stream; var stream = this.stream;

Loading…
Cancel
Save