-
Notifications
You must be signed in to change notification settings - Fork 7.3k
nextTick — specifc context #6290
Comments
nextTick(function () { }.bind(this)); ( Jorge )(); |
Something like this is currently under investigation. The difference is the |
But soon with arrow functions there's going to be no need: nextTick( () => { /* this lexically bound */ } ); |
As @xk points out, you can achieve the same thing with Function#bind() or arrow functions. I don't see a need for this. |
I don't like var iter = 1e6;
function runFast() { process.nextTick(fastNextTick); }
function fastNextTick() {
if (--iter < 0) return;
runFast();
}
runFast();
// 0:00.17elapsed 99%CPU (80616maxresident)k var iter = 1e6;
function runSlow() {
process.nextTick(function slowNextTick() {
if (--iter < 0) return;
runSlow();
});
}
runSlow();
// 0:00.29elapsed 98%CPU (151316maxresident)k function runBound() { process.nextTick(boundNexTick.bind(null)); }
function boundNexTick() {
if (--iter < 0) return;
runBound();
}
runBound();
// 0:02.21elapsed 100%CPU (472856maxresident)k var iter = 1e6;
function yourInsane() {
process.nextTick((function sinful() {
if (--iter < 0) return;
yourInsane();
}).bind(null));
}
yourInsane();
// 0:02.40elapsed 99%CPU (546276maxresident)k The reason for passing the context to the |
( Jorge )(); |
Not sure what you mean by "the receiver", but yeah. The
You're right, it is easy by declaring functions within closures so it can access the data in the bubbling scope. But it's also slower, uses more memory and not good in Node core because we're executing stuff a lot.
I'll assume you mean when you call
Eh... Did you just make a suggestion at my background in programming? The method context is a key place for storing data, and within core we're stuck creating function closures and accessing the data via the upper scopes The use case is primarily for core, but as a side effect it'd be exposed to the end-user. Greatest part is, you're still left to use whatever technique you wish to use. But in core I only propose something if it means making things faster. This will make core faster, and it's use case is core specific. |
On 01/10/2013, at 00:42, Trevor Norris wrote:
You'd have to show how would
run any faster? ( Jorge )(); |
More ugly, possibly. Faster and uses less memory, most definitely. this.err_cb = cb;
this.last_er = er;
process.nextTick(emitError);
function emitError(context) {
context.err_cb(context.last_er);
} But to be fair, that link was supposed to be this one. Since I had mentioned I had only included the "hot paths". Hopefully emitting errors isn't in your applications hot path. And if you look at the entire setup in the second link, it breaths fire and burns performance to ash. With a simple change and the new API it could look like so: Readable.prototype.resume = function resume() {
var state = this._readableState;
if (!state.flowing) {
state.flowing = true;
if (!state.reading) {
this.read(0);
}
if (!state.resumeScheduled) {
state.resumeScheduled = true;
process.nextTick(resume_)
}
}
return this;
};
function resume_(stream) {
var state = stream._readableState;
state.resumeScheduled = false;
stream.emit('resume');
flow(stream);
if (state.flowing && !state.reading)
stream.read(0);
} Now the call to |
On 01/10/2013, at 07:56, Trevor Norris wrote:
That won't work, But unless a method is in the prototype of a class, and the instance is created with a class constructor function via And the worst of all is that sometimes this kind of optimizations are rendered totally useless by some later v8 upgrade. Then what's left is a lot of effort for nothing, and the 'more ugly' part of it.
Hopefully :-)
This style where every bit of program state is kept in objects, what I called the Java-style of OOP, isn't the best suited for JS. I don't think you're going to gain many followers with this proposals because in JS, in general, we're more used to leave the program state in vars in the closures, comfily, than to save everything methodically into objects and classes. But who knows, perhaps you convince the core guys. Cheers,( Jorge )(); |
Not looking for followers. In fact I've given a few talks discussing why developers in general should not do what I do in core.
Then hallelujah, I'll update core and take advantage of the new performance optimizations offered by v8. But I'm not going to hold off making Node faster now because something's going to change 6 months down the road.
Let's keep judging what's "best suited" for JS by the results of our benchmarks. And please, refrain from making any allegations about my use of "Java-stye" anything. I fanatically despise the language with the burning of a thousand suns.
Like, um, myself? The team is usually pretty chill with my performance enhancements as long as they're not too insane and they actually offer a performance enhancement. Also, I'm pretty sure you're missing the context here. When a callback is run through Very simple change, and it would allow many more optimizations in core. |
I wish you good luck with the experiments. But please don´t uglify (á la Java) node´s source too much in your quest for speed. Cheers,( Jorge )(); |
@trevnorris ... any reason to keep this one open? |
nope |
I propose adding a second parameter to nextTick analogous to Array.prototype.map et al. which forwards a context (
this
) to the deferred function.In this context, we could quote David Herman's snippet from his book (68 Specific Ways to Harness the Power of Javascript):
Of course we could scope an instance of this (var self = this) and use it; however I'd love to see ECMA conformance here. Any objections?
The text was updated successfully, but these errors were encountered: