Giter Club home page Giter Club logo

Comments (9)

dougwilson avatar dougwilson commented on September 23, 2024

Did you find changing the arity for a deprecated function caused programs to break?

There are dumb interfaces that depend on arity, for example expressjs-style middlewares need their arities.

However, if it is a big deal, then I'd suggest instead having several wrapArity1(fn), wrapArity2(fn), and so forth, up to... 6? 10? It could be small as well.

No, the code generation can handle that. I'll definitely look into the issue. Do you know what is causing the slow down? How would I make something that would implement an arity differently? Basically the wrap tries to make the deprecated function look as close as possible to the original: it keeps fn.length the same, keeps arguments.length the same within the original function, etc.

I would welcome a suggestion or PR that meets these requirements, though :)

from nodejs-depd.

dougwilson avatar dougwilson commented on September 23, 2024

P.S. I'm going to start looking through how you are using depd in https://github.com/seanmonstar/intel to see how I can add some more convenience methods that may be more useful. So far it does look like what you are doing with __deprecatedCallback may be better off using deprecate directly instead of wrapping an empty function, but I'm still looking at your use-case and thinking about it.

from nodejs-depd.

seanmonstar avatar seanmonstar commented on September 23, 2024

I've been toying with this in a fork:

function wrapfunction(fn, message) {
  if (typeof fn !== 'function') {
    throw new TypeError('argument fn must be a function')
  }

  var deprecate = this
  var stack = getStack()
  var site = callSiteLocation(stack[1])

  site.name = fn.name

  var wrapper = arityWrappers[fn.length] || arityWrappers[0];
  return wrapper(function() {
    log.call(deprecate, message, site);
    return fn.apply(this, arguments);
  });
}

var arityWrappers = {
  '0': function arity0(fn) {
    return function() {
      return fn.apply(this, arguments);
    }
  },
  '1': function arity1(fn) {
    return function(a) {
      return fn.apply(this, arguments);
    }
  }
  '2': function arity2(fn) {
    return function(a, b) {
      return fn.apply(this, arguments);
    }
  }
  '3': function arity3(fn) {
    return function(a, b, c) {
      return fn.apply(this, arguments);
    }
  }
  '4': function arity4(fn) {
    return function(a, b, c, d) {
      return fn.apply(this, arguments);
    }
  }
}

Of course, it could extend out to a sane number of arguments (someone has a function with more than 10 arity, they should be shot?).

from nodejs-depd.

seanmonstar avatar seanmonstar commented on September 23, 2024

So far it does look like what you are doing with __deprecatedCallback may be better off using deprecate directly instead of wrapping an empty function, but I'm still looking at your use-case and thinking about it.

I have switched to using deprecate directly.

this.__deprecatedCallback = function deprecated() {
  deprecate('Handler.emit callback argument has been removed');
};
// in handle()
this.emit(record, this.__deprecatedCallback);

I need to pass a function argument, so people don't end up with undefined(). I can't pass deprecate itself, cause when my users execute the callback, it would just be generating a function, instead of leaving a warning.

from nodejs-depd.

dougwilson avatar dougwilson commented on September 23, 2024

Oh, by "use deprecate directly" I meant using to what you changed it to for now :)

So from your examples, it looks like the one-time eval is making all future calls slow?

from nodejs-depd.

seanmonstar avatar seanmonstar commented on September 23, 2024

Hrm, trying again with a different computer saw now speed lost. Differences between computes: Slow: Ubuntu 14.04, node v0.10.20. Fast: Windows 7, node v0.10.26.

process.versions says that v8 is v3.14.5.9 in both, which I'd think would be the reason for eval behaving differently...

from nodejs-depd.

seanmonstar avatar seanmonstar commented on September 23, 2024

It could just be my one computer gone crazy, as I can't repro on others (though, it continues to show on the Ubuntu.). I'd recommend closing unless it repros anywhere else.

from nodejs-depd.

dougwilson avatar dougwilson commented on September 23, 2024

I'd recommend closing unless it repros anywhere else.

The fact that you took the time to report it makes me want to at least look into it myself as well, because there could be something to it :) I don't want to close it just yet because there may well be an issue and I just haven't had the time to dig in yet.

from nodejs-depd.

dougwilson avatar dougwilson commented on September 23, 2024

@seanmonstar sorry for the long delay :( So I was able to examine it and there is a slow down. Using the wrapper is much slower per call than just calling deprecate(msg) within your function. There is a v8 trick to make them the same, though, which I'm adding. I added a simple benchmark as well so I could see that it actually made the calls faster.

from nodejs-depd.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.