高性能的算法库一般都是用C/C++编写。当你想要用JavaScript来开发条形码商业应用,你有两个选择:1.经过node-gyp来编译一个Node.js C/C++扩展。2.把C/C++代码编译成WebAssembly。这里基于Dynamsoft Barcode Reader来作一个比较。node
申请一个免费试用的序列号。git
C/C++ SDKgithub
WebAssembly SDKweb
npm i dbrjs
开发应用,性能相当重要。毫无疑问,C/C++的扩展性能确定更好。不过WebAssembly的差距到底要多大,要测试过才知道。算法
下载编译Dynamsoft Barcode Reader Node.js扩展:npm
cd src node-gyp configure node-gyp build
建立一个简单的条形码应用:json
var dbr = require('./build/Release/dbr'); var Module = require('dbrjs'); function decodeFileStreamAsync(fileName) { let stats = fs.statSync(fileName); let fileSize = stats["size"]; fs.open(fileName, 'r', function(status, fd) { if (status) { console.log(status.message); return; } var source = fs.readFileSync(fileName); var typedArray = new Uint8Array(source); Module.onRuntimeInitialized = function() { let dbr = new Module.BarcodeReaderWasm("t0068NQAAAKTSQDbEid8CTEeNluhTXi+h35G8R03xIHsyYNzZoa2GiU2a8y7s5Z1lfHsMW5dNyZmH6jQL51HUcoB5EhpDeDk="); console.time('wasm'); let results = dbr.DecodeFileInMemory(typedArray, ""); console.timeEnd('wasm'); let json = JSON.parse(results); let barcodeResults = json['textResult']; let txts = []; for (let i = 0; i < barcodeResults.length; ++i) { console.log("Value : " + Buffer.from(barcodeResults[i].BarcodeText, 'base64').toString('ascii')); } console.log("Done............................................................\n"); }; let buffer = new Buffer(fileSize); fs.read(fd, buffer, 0, fileSize, 0, function(err, bytesRead, data) { console.time('native'); dbr.decodeFileStreamAsync(buffer, fileSize, barcodeTypes, function(err, msg) { console.timeEnd('native'); let result = null; for (index in msg) { result = msg[index]; // console.log("Format: " + result['format']); console.log("Value : " + result['value']); } console.log("Done............................................................\n"); }, ""); }); }); }
运行以后能够看到性能差别:ide
WebAssembly耗时是C++扩展的3倍。性能
虽然Node.js C/C++扩展的性能占优,可是在不一样平台上必须从新编译,这样很是麻烦。而WebAssembly是没有这个问题的。在Windows上安装Linux子系统能够快速测试。测试
若是要作Web应用开发。Node.js的扩展只能用于服务端,而WebAssembly既能够用在服务端,也能够用在网页客户端。部署在服务端就须要经过HTTP来通讯,这样若是要作一个网页条形码应用,就须要不断发送数据,解码,再返回结果。很显然这样的效率是不如网页客户端直接作条形码检测的。
建立index.js:
const fs = require('fs'); var source = fs.readFileSync('test.jpg'); var typedArray = new Uint8Array(source); const Module = require('dbrjs'); Module.onRuntimeInitialized = function() { let dbr = new Module.BarcodeReaderWasm("t0068NQAAAKTSQDbEid8CTEeNluhTXi+h35G8R03xIHsyYNzZoa2GiU2a8y7s5Z1lfHsMW5dNyZmH6jQL51HUcoB5EhpDeDk="); console.time('wasm'); let results = dbr.DecodeFileInMemory(typedArray, ""); console.timeEnd('wasm'); let json = JSON.parse(results); let barcodeResults = json['textResult']; let txts = []; for (let i = 0; i < barcodeResults.length; ++i) { txts.push(Buffer.from(barcodeResults[i].BarcodeText, 'base64').toString('ascii')); } console.log(txts.join(", ")); };
运行:
node index.js
var reader; c.onRuntimeInitialized = function () { document.getElementById('anim-loading').style.display = 'none'; buttonFile.disabled = false; buttonVideo.disabled = false; reader = new c.BarcodeReaderWasm("t0068NQAAAKTSQDbEid8CTEeNluhTXi+h35G8R03xIHsyYNzZoa2GiU2a8y7s5Z1lfHsMW5dNyZmH6jQL51HUcoB5EhpDeDk="); }; if (reader) { try { // results = reader.DecodeBuffer(idd.buffer, imageWidth, imageHeight, imageWidth * 4, 7, ""); let results = reader.DecodeFileInMemory(arrayBuffer, ""); let json = JSON.parse(results); let barcodeResults = json['textResult']; let txts = []; for (let i = 0; i < barcodeResults.length; ++i) { txts.push(b64DecodeUnicode(barcodeResults[i].BarcodeText)); } barcode_result.textContent = txts.join(", "); } catch (e) { console.log(e); } }
若是你要开发服务端的应用,并且追求性能,选择Node.js C/C++扩展
。若是不是太在乎性能上的那点差别,WebAssembly
确定是最佳选择。