metafloor / bwip-js

Barcode Writer in Pure JavaScript
Other
2.11k stars 304 forks source link

Speed up generate barcode #18

Closed dannyto closed 9 years ago

dannyto commented 9 years ago

Hi there!

Can we generate barcode a little bit faster. Each barcode take about one second, how can we generate more barcode with less time.

Thankz!

metafloor commented 9 years ago

Which bar code types are you generating? If a 2D symbol like QR-Code, there is a lot of processing required for the error correction bits. 1D symbols like code128 should render much faster.

dannyto commented 9 years ago

I'm using 2D symbol like QR-Code and DataMatrix. So when i generate barcode with value from 1 to 100000, it take a long time. Is there anyway to make it faster for generate?

Thankz!

terryburton commented 9 years ago

Is Data Matrix too slow, or just QR Code specifically?

The error correction isn't particularly expensive however QR Code has a very expensive mask evaluation routine to determine which of a set of masks when applied results in a symbol with the least number of undesirable image artefacts. The PS code is about as well optimised as you'll get for this but it's still a performance killer.

One improvement would be allow the user to specify a particular mask and skip the evaluation - not entirely to specification (most other implementations I've seen get the scoring wrong anyway) but it will speed things up and the symbols will still be valid. If this would be helpful then let me know and I'll implement.

dannyto commented 9 years ago

Data Matrix is fine, just QR Code. Please help me if you have away to solve it! Thankz.

terryburton commented 9 years ago

@metafloor BWIPP commit 19cb9147aac2ff9519f70ec7f0eeb48060f2be1f introduces an undocumented mask option (1-8 for QR Code, 1-4 for Micro QR Code) that allows the user to pick a particular mask in order to avoid the cost of evaluating each mask for find the optimal. Ignoring the rendering, it reduces the time taken to generate a version 40 symbol from 810ms to 335ms on my system. That's about as good as you can get without micro-optimising a lot of the code for somewhat less gain.

I'm not proud of this as it reduces the quality of the QR Code output so will not cut a release based on this alone, but it's no worse that what a lot of generators do. Some experimentation might be required to determine whether some masks work better than others before settling on one in particular.

It's up to you whether you want to cherry pick it into BWIP-JS or await some future release. I'm choosing not to document the option. You might do likewise!

metafloor commented 9 years ago

Attached is the replacement bwipp/qrcode.js file that incorporates the special one-off change (thanks Terry!). Copy it to your bwip-js/bwipp directory. This will not become part of a bwip-js release until some later date.

Github won't allow uploading .js files so make sure to change the file extension from .txt back to .js. qrcode.txt

dannyto commented 9 years ago

Thank you so much you guy!