Permalink
Browse files

Add different functions for LE and BE lookups for performance

  • Loading branch information...
1 parent 8a58a8d commit faa3cff23923ab9771a9725a44e7b2da72cd74eb @Trubbs Trubbs committed Apr 14, 2012
Showing with 54 additions and 77 deletions.
  1. +1 −1 test/index.js
  2. +53 −76 view-buffer.js
View
@@ -23,7 +23,7 @@ test('basics', function(t){
view: 'Uint8',
bytes: 1,
length: 1,
- endian: 'little',
+ endian: 'LE',
excess: 0,
bytesEach: 1
}, 'correct starting properties');
View
@@ -89,7 +89,7 @@ ViewBuffer.prototype = {
reverse: Array.prototype.reverse,
join: Array.prototype.join,
- endian: 'little',
+ endian: 'LE',
subarray: function subarray(start, end, view){
if (typeof start === 'string' && isNaN(start)) {
@@ -185,7 +185,9 @@ ViewBuffer.prototype = {
if (!target) {
target = new ViewBuffer(length);
target.view = this.view;
- target.endian = this.endian;
+ if (this.hasOwnProperty('endian')) {
+ target.endian = this.endian;
+ }
} else if (!Array.isArray(target)) {
if (targetOffset + length > target.length) {
length = target.length;
@@ -213,10 +215,10 @@ var handler = {
return numbers(target.length).concat(fwd().filter(function(s){ return !(s in Object.prototype) }));
},
keys: function(fwd, target){
- return numbers(Math.min(ViewBuffer.INSPECT_MAX_BYTES, target.length)).concat(fwd());
+ return numbers(target.length).concat(fwd());
},
has: function(fwd, target, name){
- return name < target.length || fwd();
+ return isFinite(name) ? name < target.length : fwd();
},
owns: function(fwd, target, name){
return isFinite(name) ? name < target.length : fwd();
@@ -243,12 +245,21 @@ var handler = {
if (has(nameMap, val)) val = nameMap[val];
if (!has(types, val)) throw new Error('Unknown data type');
var bytesEach = types[val][0];
- target.get = getters[val];
- target.set = setters[val];
+ target.get = getters[val] || getters[val+target.endian];
+ target.set = setters[val] || setters[val+target.endian];
target.view = val;
target.length = target.bytes / bytesEach | 0;
target.excess = target.bytes % bytesEach;
target.bytesEach = bytesEach;
+ } else if (name === 'endian') {
+ if (val !== 'LE' && val !== 'BE') {
+ throw new TypeError('Endian must be either "LE" or "BE"');
+ }
+ if (getters[target.view+val]) {
+ target.get = getters[target.view+val];
+ target.set = setters[target.view+val];
+ }
+ fwd();
} else {
fwd();
}
@@ -257,84 +268,50 @@ var handler = {
};
var getters = {
- Ascii: function getAscii(i){
- return String.fromCharCode(this.data.getUint8(i));
- },
- Ucs2: function getUcs2(i){
- return ucs2(this.data.getUint16(i * 2));
- },
- Hex: function getHex(i){
- return ('00'+this.data.getUint8(i).toString(16)).slice(-2);
- },
- Binary: function getBinary(i){
- return this.data.getUint8(i / 8 | 0) & powers[i] ? 1 : 0;
- },
- Uint8: function getUint8(i){
- return this.data.getUint8(i);
- },
- Int8: function getInt8(i){
- return this.data.getInt8(i);
- },
- Uint16: function getUint16(i){
- return this.data.getUint16(i * 2, this.endian === 'little');
- },
- Int16: function getInt16(i){
- return this.data.getInt16(i * 2, this.endian === 'little');
- },
- Uint32: function getUint32(i){
- return this.data.getUint32(i * 4, this.endian === 'little');
- },
- Int32: function getInt32(i){
- return this.data.getInt32(i * 4, this.endian === 'little');
- },
- Float32: function getFloat32(i){
- return this.data.getFloat32(i * 2, this.endian === 'little');
- },
- Float64: function getFloat64(i){
- return this.data.getFloat64(i * 4, this.endian === 'little');
- },
+ Ascii: function getAscii(i){ return String.fromCharCode(this.data.getUint8(i)) },
+ Ucs2: function getUcs2(i){ return ucs2(this.data.getUint16(i * 2)) },
+ Hex: function getHex(i){ return ('00'+this.data.getUint8(i).toString(16)).slice(-2) },
+ Binary: function getBinary(i){ return this.data.getUint8(i / 8 | 0) & powers[i] ? 1 : 0 },
+ Uint8: function getUint8(i){ return this.data.getUint8(i) },
+ Int8: function getInt8(i){ return this.data.getInt8(i) },
+ Uint16LE: function getUint16LE(i){ return this.data.getUint16(i * 2, true) },
+ Int16LE: function getInt16LE(i){ return this.data.getInt16(i * 2, true) },
+ Uint32LE: function getUint32LE(i){ return this.data.getUint32(i * 4, true) },
+ Int32LE: function getInt32LE(i){ return this.data.getInt32(i * 4, true) },
+ Float32LE: function getFloat32LE(i){ return this.data.getFloat32(i * 2, true) },
+ Float64LE: function getFloat64LE(i){ return this.data.getFloat64(i * 4, true) },
+ Uint16BE: function getUint16BE(i){ return this.data.getUint16(i * 2) },
+ Int16BE: function getInt16BE(i){ return this.data.getInt16(i * 2) },
+ Uint32BE: function getUint32BE(i){ return this.data.getUint32(i * 4) },
+ Int32BE: function getInt32BE(i){ return this.data.getInt32(i * 4) },
+ Float32BE: function getFloat32BE(i){ return this.data.getFloat32(i * 2) },
+ Float64BE: function getFloat64BE(i){ return this.data.getFloat64(i * 4) }
};
var setters = {
- Ascii: function setAscii(i,v){
- return this.data.setUint8(i, typeof v === 'string' ? v.charCodeAt(0) : v);
- },
- Ucs2: function setUcs2(i,v){
- return this.data.setUint16(i * 2, typeof v === 'string' ? ucs2(v) : v);
- },
- Hex: function setUcs2(i,v){
- return this.data.setUint8(i, Number('0x'+v));
- },
+ Ascii: function setAscii(i,v){ return this.data.setUint8(i, typeof v === 'string' ? v.charCodeAt(0) : v) },
+ Ucs2: function setUcs2(i,v){ return this.data.setUint16(i * 2, typeof v === 'string' ? ucs2(v) : v) },
+ Hex: function setUcs2(i,v){ return this.data.setUint8(i, Number('0x'+v)) },
Binary: function setBinary(i,v){
var power = powers[i];
var current = this.data.getUint8(i = i / 8 | 0);
return this.data.setUint8(i, v ? current | power : current & ~power);
},
- Uint8: function setUint8(i,v){
- return this.data.setUint8(i, v);
- },
- Int8: function setInt8(i,v){
- return this.data.setInt8(i, v);
- },
- Uint16: function setUint16(i,v){
- return this.data.setUint16(i * 2, v, this.endian === 'little');
- },
- Int16: function setInt16(i,v){
- return this.data.setInt16(i * 2, v, this.endian === 'little');
- },
- Uint32: function setUint32(i,v){
- return this.data.setUint32(i * 4, v, this.endian === 'little');
- },
- Int32: function setInt32(i,v){
- return this.data.setInt32(i * 4, v, this.endian === 'little');
- },
- Float32: function setFloat32(i,v){
- return this.data.setFloat32(i * 2, v, this.endian === 'little');
- },
- Float64: function setFloat64(i,v){
- return this.data.setFloat64(i * 4, v, this.endian === 'little');
- },
-}
+ Uint8: function setUint8(i,v){ return this.data.setUint8(i, v) },
+ Int8: function setInt8(i,v){ return this.data.setInt8(i, v) },
+ Uint16LE: function setUint16LE(i,v){ return this.data.setUint16(i * 2, v, true) },
+ Int16LE: function setInt16LE(i,v){ return this.data.setInt16(i * 2, v, true) },
+ Uint32LE: function setUint32LE(i,v){ return this.data.setUint32(i * 4, v, true) },
+ Int32LE: function setInt32LE(i,v){ return this.data.setInt32(i * 4, v, true) },
+ Float32LE: function setFloat32LE(i,v){ return this.data.setFloat32(i * 2, v, true) },
+ Float64LE: function setFloat64LE(i,v){ return this.data.setFloat64(i * 4, v, true)},
+ Uint16BE: function setUint16BE(i,v){ return this.data.setUint16(i * 2, v) },
+ Int16BE: function setInt16BE(i,v){ return this.data.setInt16(i * 2, v) },
+ Uint32BE: function setUint32BE(i,v){ return this.data.setUint32(i * 4, v) },
+ Int32BE: function setInt32BE(i,v){ return this.data.setInt32(i * 4, v) },
+ Float32BE: function setFloat32BE(i,v){ return this.data.setFloat32(i * 2, v) },
+ Float64BE: function setFloat64BE(i,v){ return this.data.setFloat64(i * 4, v) },
+};

0 comments on commit faa3cff

Please sign in to comment.