Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slow loading times #810

Open
sebamarynissen opened this issue Apr 27, 2018 · 1 comment
Open

Slow loading times #810

sebamarynissen opened this issue Apr 27, 2018 · 1 comment

Comments

@sebamarynissen
Copy link

When loading the library using

require('pdkit');

the loading is horribly slow. I always get load times of about 500ms. Given that node's require is synchronous, this means 500ms of blocking.

I found out that the slow load times are being caused by the fontkit dependency. I propose to fix it by only requiring fontkit "on the fly", so

class PDFFont
  @open: (document, src, family, id) ->

    # Only require fontkit when we absolutely need it.
    fontkit = require 'fontkit'
    if typeof src is 'string'
      if StandardFont.isStandardFont src
        return new StandardFont document, src, id
      font = fontkit.openSync src, family
    else if Buffer.isBuffer(src)
      font = fontkit.create src, family
    else if src instanceof Uint8Array
      font = fontkit.create new Buffer(src), family
    else if src instanceof ArrayBuffer
      font = fontkit.create new Buffer(new Uint8Array(src)), family

instead of

fontkit = require 'fontkit'
class PDFFont
  @open: (document, src, family, id) ->
    if typeof src is 'string'
      if StandardFont.isStandardFont src
        return new StandardFont document, src, id
      font = fontkit.openSync src, family
    else if Buffer.isBuffer(src)
      font = fontkit.create src, family
    else if src instanceof Uint8Array
      font = fontkit.create new Buffer(src), family
    else if src instanceof ArrayBuffer
      font = fontkit.create new Buffer(new Uint8Array(src)), family

I tested it and it solves my issue. Going to file a pull-request for this.

sebamarynissen added a commit to sebamarynissen/pdfkit that referenced this issue Apr 27, 2018
Only require fontkit on the fly to fix slow loading times.
@jimmywarting
Copy link

maybe updating fontkit to v2 will speed things up, the deep-equal pkg is insanely large. it got replaced with fast-deep-equal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants