From 88abdae6cdddcefc422a3c4d9eb5e1e4785583f7 Mon Sep 17 00:00:00 2001 From: tangyao Date: Wed, 18 Nov 2015 18:24:51 +0800 Subject: [PATCH] fix: readFloat 2.3 => 2.299 --- benchmark/buffer_toString_fromCharCode.js | 61 +++++++++++++++++++++++ lib/byte.js | 4 +- test/byte.test.js | 12 ++++- 3 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 benchmark/buffer_toString_fromCharCode.js diff --git a/benchmark/buffer_toString_fromCharCode.js b/benchmark/buffer_toString_fromCharCode.js new file mode 100644 index 0000000..dd9ed82 --- /dev/null +++ b/benchmark/buffer_toString_fromCharCode.js @@ -0,0 +1,61 @@ +/**! + * byte - benchmark/buffer_toString_fromCharCode.js + * + * Copyright(c) tangyao and other contributors. + * MIT Licensed + * + * Authors: + * tangyao <2001-wms@163.com> + */ + +'use strict'; + +/** + * Module dependencies. + */ + +var Benchmark = require('benchmark'); +var benchmarks = require('beautify-benchmark'); + +function getRawString(_bytes, index, length) { + var data = []; + for (var pos = index, end = index + length; pos < end; pos++) { + var ch = _bytes[pos]; + if (ch < 0x80) { + data.push(ch); + } else if ((ch & 0xe0) === 0xc0) { + var ch1 = _bytes[++pos]; + var v = ((ch & 0x1f) << 6) + (ch1 & 0x3f); + data.push(v); + } else if ((ch & 0xf0) === 0xe0) { + var ch1 = _bytes[++pos]; + var ch2 = _bytes[++pos]; + var v = ((ch & 0x0f) << 12) + ((ch1 & 0x3f) << 6) + (ch2 & 0x3f); + data.push(v); + } + } + return String.fromCharCode.apply(null, data); +} + +var suite = new Benchmark.Suite(); + +var buffer = new Buffer('eda0bdedb880577777e982a3', 'hex'); + +suite +.add('buffer.toString', function () { + buffer.toString('utf8', 0, 12); +}) +.add('getRawString', function () { + getRawString(buffer, 0, 12); +}) + +.on('cycle', function(event) { + benchmarks.add(event.target); +}) +.on('start', function(event) { + console.log('\n node version: %s, date: %s\n Starting...', process.version, Date()); +}) +.on('complete', function done() { + benchmarks.log(); +}) +.run({ 'async': false }); diff --git a/lib/byte.js b/lib/byte.js index 07d4d00..b5bd8ea 100644 --- a/lib/byte.js +++ b/lib/byte.js @@ -170,6 +170,7 @@ Object.keys(numbers).forEach(function (type) { var getMethod = 'get' + type; var handles = numbers[type]; var size = handles.size; + var isFloat = type === 'Float'; ByteBuffer.prototype[putMethod] = function (index, value) { // index, value @@ -198,7 +199,8 @@ Object.keys(numbers).forEach(function (type) { var handle = this._order === BIG_ENDIAN ? handles.readBE : handles.readLE; - return this._bytes[handle](index); + var rs = this._bytes[handle](index); + return isFloat ? Number(rs.toLocaleString()) : rs; }; }); diff --git a/test/byte.test.js b/test/byte.test.js index cd25293..bbaa398 100644 --- a/test/byte.test.js +++ b/test/byte.test.js @@ -18,6 +18,7 @@ var should = require('should'); var Long = require('long'); var ByteBuffer = require('../'); +var debug = require('debug')('byte'); describe('byte.test.js', function () { describe('new ByteBuffer()', function () { @@ -134,16 +135,23 @@ describe('byte.test.js', function () { bytes.getFloat().should.equal(0); cases.forEach(function (item) { bytes.order(ByteBuffer.BIG_ENDIAN); + debug('putFloat', item[0]); bytes.putFloat(0, item[0]); bytes.toString().should.equal(item[1]); - String(bytes.getFloat(0)).should.containEql(item[0]); + bytes.getFloat(0).should.containEql(item[0]); bytes.order(ByteBuffer.LITTLE_ENDIAN); bytes.putFloat(0, item[0]); bytes.toString().should.equal(item[2]); - String(bytes.getFloat(0)).should.containEql(item[0]); + bytes.getFloat(0).should.containEql(item[0]); }); + + bytes.position(0); + bytes.putFloat(2.3); + bytes.getFloat(0).should.eql(2.3); + }); + }); describe('putInt(), getInt()', function () {