JavaScript條形碼SDK比較:Node.js C/C++擴展 vs WebAssembly

高性能的算法庫一般都是用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

Node.js條形碼掃描

WebAssembly耗時是C++擴展的3倍。性能

可移植性

雖然Node.js C/C++擴展的性能佔優,可是在不一樣平臺上必須從新編譯,這樣很是麻煩。而WebAssembly是沒有這個問題的。在Windows上安裝Linux子系統能夠快速測試。測試

WebAssembly Windows Linux

Web應用開發

若是要作Web應用開發。Node.js的擴展只能用於服務端,而WebAssembly既能夠用在服務端,也能夠用在網頁客戶端。部署在服務端就須要經過HTTP來通訊,這樣若是要作一個網頁條形碼應用,就須要不斷髮送數據,解碼,再返回結果。很顯然這樣的效率是不如網頁客戶端直接作條形碼檢測的。

WebAssembly條形碼開發示例

Node.js

建立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

Web

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);
      }
}

Dynamsoft Barcode Reader WebAssembly

結論

若是你要開發服務端的應用,並且追求性能,選擇Node.js C/C++擴展。若是不是太在乎性能上的那點差別,WebAssembly確定是最佳選擇。

源碼

https://github.com/dynamsoft-dbr/webassembly

相關文章
相關標籤/搜索