8000 Pass instance into `oncreate` as argument, or `.call` it · Issue #557 · sveltejs/svelte · GitHub
[go: up one dir, main page]

Skip to content
Pass instance into oncreate as argument, or .call it #557
Closed
@Rich-Harris

Description

@Rich-Harris

Follow-up to #525 (comment).bind is slow and we should avoid it.

The code in question is here:

options._root._renderHooks.push( template.oncreate.bind( this ) );

We have a few options:

Go back to passing the context alongside the function

options._root._renderHooks.push({ fn: template.oncreate, context: this });

Not totally ideal, since a) you might not have a context, and b) if we were to include additional arguments (#550) then you'd have to deal with those too.

Wrap the function with a .call

var self = this;
options._root._renderHooks.push( function () {
  template.oncreate.call( self );
});

Rewrite the function so that it takes the instance as an argument

var self = this;
options._root._renderHooks.push( function () {
  template.oncreate( self );
});

I poked around a bit the other night and I don't actually think that's any faster than .call, so probably not worth it.

I'm also not certain that wrapping the function is quicker than .bind, we'd have to check. If not, then maybe we do need to consider going back to the old way of doing things.

Metadata

Metadata

Assignees

No one assigned

    Labels

    awaiting submitterneeds a reproduction, or clarification

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0