Its called a poor mans implementation for a reason. Its not a real hash as we know it. But by abusing array prototypes and objects we can get real close to the concept of hashes. Research is key here. 😀
Poor implementation of plugin aliasing?
Learning JavascriptPersonally, I dont really see how something like this...
var opluginCommand = Game_Interpreter.prototype.pluginCommand;Game_Interpreter.prototype.pluginCommand = function(command, args) { if (command.contains("my_custom_command")){ // Call my specific plugin command passing only args to function. }else{ opluginCommand.apply(this, arguments); }};could have too much of a negative impact. 🙂
var opluginCommand = Game_Interpreter.prototype.pluginCommand;Game_Interpreter.prototype.pluginCommand = function(command, args) { if (command.contains("my_custom_command")){ // Call my specific plugin command passing only args to function. }else{ opluginCommand.apply(this, arguments); }};could have too much of a negative impact. 🙂
If your plugin was the first on the list, and the user had 200 other plugins installed all using plugin command, it would only find yours after calling 200 methods.
In the worst case, if they grouped all of your plugins together at the top, that means all of your plugin commands will take the longest to process.
Of course, this is only the theoretical implications of the alias method calling approach and will only be a problem with "sufficiently large" number of method calls. A couple thousand calls? No sweat. As you say, perhaps in reality, this stuff makes no difference and we shouldn't really worry about it.
If people were to register plugin commands for example, and some plugin command manager handled the calls, then it would be a single look-up which would be much faster.
aliasing should only be used where applicable, else just extend the object and create your own methods or create your own classes.
Is there a way to extend the object and create our own methods, and use those in plugin commands, without aliasing?
Not without using the partly finished/compatible ES6 commands, so atm, not really.
Yea, thats a point I guess..
Perhaps it would be best for some kinda plugin manager type thing. I mean, it would be easy enough to have some register function that adds a unique identifier into some object, and then within the plugin command function, just reference said unique id, and call the function associated with that id. Then it would be a VERY fast way of searching, with no loops, and no re-calling of the alias functions. 🙂
Maybe something like...
(function(terp){ var uniqueIdentifiers = {}; terp.registerCommand = function(uniqueID, idFunk){ if (uniqueIdentifiers[uniqueID] === undefined){ uniqueIdentifiers[uniqueID] = idFunk; return true; } return false; } var opluginCommand = terp.pluginCommand; terp.pluginCommand = function(command, args) { if (uniqueIdentifiers[command] !== undefined){ uniqueIdentifiers[command].apply(this, args); } else { opluginCommand.apply(this, arguments); } }; terp.myCustomCommandFunk = function(){ // Some code to process plugin command } })(Game_Interpreter.prototype);And then you would register the command with something like:
terp = Game_Interpreter.prototype; terp.registerCommand('mycustomcommand', terp.myCustomCommandFunk);Note, I've not tested that, its just a thought, but now that I think of it, it would probably be best to have something like this implemented. 🙂
uh .... You can do it in es5 too. With prototypes ...
Thats what we are doing now, I think he ment another way.
And afaik there isn't one until the ES6 'extends'.
I wouldn't extend the class for something like this, since you end up filling the prototype with stuff that could as well be inside your anonymous function, like this:
Obviously, this way other plugins won't see your own function and can't override/extend it, but that might very well be intented.
Code:
(function() { var myFunc = function() { // ... }; var _alias_pluginCommand = Game_Interpreter.prototype.pluginCommand; Game_Interpreter.prototype.pluginCommand = function(command, args) { if(...) { return myFunc(args); } _alias_pluginCommand.apply(this, arguments); } })();
Which you can do now, and use compilers for.
Couple things that bug me about this:
_alias_pluginCommand //=> aliasPluginCommand// or even better😮ldPluginCommandMethodone other thing that bugs me:
Game_Interpreter.prototype.pluginCommand = function(command, args) { if(...) { return myFunc(args); } _alias_pluginCommand.apply(this, arguments); }// sweet sweet line breaksI try and alias as less as possible due to the fact that as some one pointed out you have to go up and up and up and up the chain and eventually it becomes a performance issue and compatibility and this and that can become a nightmare ...
In this case i wouldn't bother too much about performance, because plugin commands usually aren't called that often and you would much sooner run into problems with extended "update()" und similar functions, that are called every frame.
I tend to prefix my variables with an underscore to make it easier to separate them from functions. Just a habit of mine. I don't see a problem with the linebreaks, either, but i generally never write long functions, so my code is better readable by default.
I tend to prefix my variables with an underscore to make it easier to separate them from functions. Just a habit of mine. I don't see a problem with the linebreaks, either, but i generally never write long functions, so my code is better readable by default.
And you're sure that has no effect on performance?
I mean when I look at your plugins, I can't even read the source one, because it seems so alien (thats probably due to not having looked at ES6 really, so thats ok) but the compiled one...oh my lord lol.
I mean a mess doesn't even cut it and its so huge, like 100 lines source, 3500 compiled...wtf is that XD
You seem to be a professional coder, so I guess everything works fine, even if it looks like crap, but I won't even bother with looking at that stuff until there is a clean implementation with widespread compatability.
Also the thing about using underscore for variables.
Most of the code I've seen so far uses underscore for private variables and I think thats a huge help, always knowing what is what at first glance.
I mean I understand why it bothers you, beeing used to camelcase and the language itself beeing setup like that, but atleast one underscore (as in _var) for privat variables seems really helpful to me.
I made a simple jsperf comparing the time it takes to execute aliased methods and evals. Even though it is still very fast, aliasing is really slow when compared to simple evals.
Here's the url: http://jsperf.com/aliased-methods/11
Here's the url: http://jsperf.com/aliased-methods/11
Since that eval is constant, it's likely that the compiler caches the result, though.
Ah, that makes sense.
this part would not make sense, eval is done on the fly. Why would it cache ...
Well, I tried to change the evaled string at every iteration, but it didn't made any difference. I checked other perfs and apparently it's okay to call eval on jsperf without any concern for cache.
Hmmm I thought one of my scripts where I evalued an input I removed it from the cache because I personally think in most situations evals should not be cached. Ill have to track that down.
A quick google search give me no result for anything related to eval cache, I guess the results aren't really cached.
Original thread: https://forums.rpgmakerweb.com/threads/poor-implementation-of-plugin-aliasing.50237/