[Pkg-javascript-commits] [node-iconv-lite] 46/83: Version 0.2.5: Optimization of table & internal encodings.
matthew pideil
mpideil-guest at moszumanska.debian.org
Tue Apr 1 19:56:49 UTC 2014
This is an automated email from the git hooks/post-receive script.
mpideil-guest pushed a commit to branch master
in repository node-iconv-lite.
commit 9fca2317fe85268f69781bbf8bf66162c1434fae
Author: Alexander Shtuchkin <ashtuchkin at gmail.com>
Date: Sun Aug 26 06:52:01 2012 +0400
Version 0.2.5: Optimization of table & internal encodings.
---
index.js | 31 +++++++++++--------------------
package.json | 2 +-
test/perfomance.js | 10 +++++-----
3 files changed, 17 insertions(+), 26 deletions(-)
diff --git a/index.js b/index.js
index bd4858b..d0b8125 100644
--- a/index.js
+++ b/index.js
@@ -75,8 +75,6 @@ var iconv = module.exports = {
}
return {
- // Seems that V8 is not optimizing functions if they are created again and again.
- // TODO: Make same optimization for other encodings.
toEncoding: toSingleByteEncoding,
fromEncoding: fromSingleByteEncoding,
options: options,
@@ -89,10 +87,14 @@ var iconv = module.exports = {
throw new Error("Encoding '" + options.type + "' has incorect 'table' option");
}
if (!options.revCharsTable) {
- options.revCharsTable = {};
+ var revCharsTable = options.revCharsTable = {};
+ for (var i = 0; i <= 0xFFFF; i++) {
+ revCharsTable[i] = 0;
+ }
+
var table = options.table;
for (var key in table) {
- options.revCharsTable[table[key]] = parseInt(key, 10);
+ revCharsTable[table[key]] = +key;
}
}
@@ -116,14 +118,8 @@ function fromInternalEncoding(buf) {
function toTableEncoding(str) {
str = ensureString(str);
var strLen = str.length;
- var bufLen = strLen;
- for (var i = 0; i < strLen; i++) {
- if (str.charCodeAt(i) >> 7) {
- bufLen++;
- }
- }
var revCharsTable = this.options.revCharsTable;
- var newBuf = new Buffer(bufLen), gbkcode, unicode,
+ var newBuf = new Buffer(strLen*2), gbkcode, unicode,
defaultChar = revCharsTable[iconv.defaultCharUnicode.charCodeAt(0)];
for (var i = 0, j = 0; i < strLen; i++) {
@@ -136,19 +132,14 @@ function toTableEncoding(str) {
newBuf[j++] = unicode;
}
}
- return newBuf;
+ return newBuf.slice(0, j);
}
function fromTableEncoding(buf) {
buf = ensureBuffer(buf);
- var bufLen = buf.length, strLen = 0;
- for (var i = 0; i < bufLen; i++) {
- strLen++;
- if (buf[i] & 0x80) //the high bit is 1, so this byte is gbkcode's high byte.skip next byte
- i++;
- }
+ var bufLen = buf.length;
var table = this.options.table;
- var newBuf = new Buffer(strLen*2), unicode, gbkcode,
+ var newBuf = new Buffer(bufLen*2), unicode, gbkcode,
defaultChar = iconv.defaultCharUnicode.charCodeAt(0);
for (var i = 0, j = 0; i < bufLen; i++, j+=2) {
@@ -162,7 +153,7 @@ function fromTableEncoding(buf) {
newBuf[j] = unicode & 0xFF; //low byte
newBuf[j+1] = unicode >> 8; //high byte
}
- return newBuf.toString('ucs2');
+ return newBuf.slice(0, j).toString('ucs2');
}
function toSingleByteEncoding(str) {
diff --git a/package.json b/package.json
index d31d052..19e5535 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
{
"name": "iconv-lite",
"description": "Convert character encodings in pure javascript.",
- "version": "0.2.4",
+ "version": "0.2.5",
"keywords": ["iconv", "convert", "charset"],
"author": "Alexander Shtuchkin <ashtuchkin at gmail.com>",
diff --git a/test/perfomance.js b/test/perfomance.js
index f6c6105..835deac 100644
--- a/test/perfomance.js
+++ b/test/perfomance.js
@@ -3,12 +3,12 @@ var iconv = require('iconv');
var iconv_lite = require("../index");
var encoding = process.argv[2] || "windows-1251";
-var convertTimes = 1000;
+var convertTimes = 10000;
var encodingStrings = {
'windows-1251': 'This is a test string 32 chars..',
- 'gbk': '这是中文字符测试。。!@¥%',
- 'utf8': '这是中文字符测试。。!@¥%This is a test string 32 chars..',
+ 'gbk': '这是中文字符测试。。!@¥%12',
+ 'utf8': '这是中文字符测试。。!@¥%12This is a test string 48 chars..',
};
// Test encoding.
var str = encodingStrings[encoding];
@@ -28,7 +28,7 @@ for (var i = 0; i < convertTimes; i++) {
var b = converter.convert(str);
}
var duration = Date.now() - start;
-var mbs = convertTimes*str.length/duration/1024;
+var mbs = convertTimes*b.length/duration/1024;
console.log("iconv: "+duration+"ms, "+mbs.toFixed(2)+" Mb/s.");
@@ -37,7 +37,7 @@ for (var i = 0; i < convertTimes; i++) {
var b = iconv_lite.encode(str, encoding);
}
var duration = Date.now() - start;
-var mbs = convertTimes*str.length/duration/1024;
+var mbs = convertTimes*b.length/duration/1024;
console.log("iconv-lite: "+duration+"ms, "+mbs.toFixed(2)+" Mb/s.");
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-iconv-lite.git
More information about the Pkg-javascript-commits
mailing list