Skip to content

Commit

Permalink
Use defineProperty on prototype instead of __defineGetter__
Browse files Browse the repository at this point in the history
This vastly improves performance by avoiding v8 tossing the
entire Reader object into the slow lookup path.
  • Loading branch information
arekinath committed Sep 30, 2015
1 parent 38b423e commit 50afd88
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
28 changes: 18 additions & 10 deletions lib/ber/reader.js
Expand Up @@ -24,18 +24,26 @@ function Reader(data) {
// These hold the "current" state
this._len = 0;
this._offset = 0;

var self = this;
this.__defineGetter__('length', function() { return self._len; });
this.__defineGetter__('offset', function() { return self._offset; });
this.__defineGetter__('remain', function() {
return self._size - self._offset;
});
this.__defineGetter__('buffer', function() {
return self._buf.slice(self._offset);
});
}

Object.defineProperty(Reader.prototype, 'length', {
enumerable: true,
get: function () { return (this._len); }
});

Object.defineProperty(Reader.prototype, 'offset', {
enumerable: true,
get: function () { return (this._offset); }
});

Object.defineProperty(Reader.prototype, 'remain', {
get: function () { return (this._size - this._offset); }
});

Object.defineProperty(Reader.prototype, 'buffer', {
get: function () { return (this._buf.slice(this._offset)); }
});


/**
* Reads a single byte and advances offset; you can pass in `true` to make this
Expand Down
16 changes: 8 additions & 8 deletions lib/ber/writer.js
Expand Up @@ -50,16 +50,16 @@ function Writer(options) {
// A list of offsets in the buffer where we need to insert
// sequence tag/len pairs.
this._seq = [];

var self = this;
this.__defineGetter__('buffer', function() {
if (self._seq.length)
throw new InvalidAsn1Error(self._seq.length + ' unended sequence(s)');

return self._buf.slice(0, self._offset);
});
}

Object.defineProperty(Writer.prototype, 'buffer', {
get: function () {
if (this._seq.length)
throw new InvalidAsn1Error(this._seq.length + ' unended sequence(s)');

return (this._buf.slice(0, this._offset));
}
});

Writer.prototype.writeByte = function(b) {
if (typeof(b) !== 'number')
Expand Down

0 comments on commit 50afd88

Please sign in to comment.