Skip to content

Change return type of parse#53

Open
jamiemlaw wants to merge 1 commit into
rawify:mainfrom
jamiemlaw:parse
Open

Change return type of parse#53
jamiemlaw wants to merge 1 commit into
rawify:mainfrom
jamiemlaw:parse

Conversation

@jamiemlaw
Copy link
Copy Markdown

This PR aims to reduce the size of the library by changing the return type of the parse function. Previously, it returned a new Complex; now, it returns a [re, im] tuple. This then allows us to write things like:

const [re, im] = parse(a, b);

and use re and im as variables, rather than z['re'] and z['im'], saving quite a few bytes.

By my calculations this reduces the minified size from 9861 to 9671 bytes (a roughly 2% saving); and after GZIP, from 2890 to 2845 bytes. It also reduces the overhead of instantiating a new Complex object with each parse.

I was reticent at first to use destructuring assignment, in case it reduced browser compatibility in some way, but I see that in the hypot function, it's already used, so I don't think it'll be an issue.

Happy to hear your thoughts.

@donhatch
Copy link
Copy Markdown

My first thought is that the proposal makes it so parse() still has to do a memory allocation for the return value (a 2-element array). If optimization (or allowing future optimization) is the goal, a better signature might be to make it a member function of Complex:
Complex c;
c.parse(a, b)
That would allow a caller to reuse c for multiple parse() calls, without suffering any memory allocations at all.

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

Successfully merging this pull request may close these issues.

2 participants