Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instanceof and architecture for extension #5673

Closed
sunag opened this issue Nov 22, 2014 · 4 comments
Closed

instanceof and architecture for extension #5673

sunag opened this issue Nov 22, 2014 · 4 comments

Comments

@sunag
Copy link
Collaborator

sunag commented Nov 22, 2014

Hi @mrdoob.

Have you ever thought of changing the architecture with prototype to reduce the number of instanceof on the code? This would help to extend and optimizes the project for many instances.

Another thing also applied to shaders. At the time I used wanted to create a Layered Texture as I did in Flash (SEA3D) but found no alternative to extends without change the root code. Today this is possible with the new changes in r69 from prototype class?

My benchmarking about instanceof and prototype:

function Obj() {}
Obj.prototype = {
    constructor: Obj,
    fn : function () { }    
}

// PROTOTYPE (Naturally is dynamic)
var n = new Date().getTime();
var i = 0;
var obj = new Obj();
while(i++ < 100000000)
{
    obj.fn();
}
console.log(new Date().getTime() - n);

// SINGLE INSTANCEOF
var n = new Date().getTime();
var i = 0;
while(i++ < 100000000)
{
    if (i instanceof Date)
    {
    }
}
console.log(new Date().getTime() - n);

// MULTIPLES INSTANCEOF
var n = new Date().getTime();
var i = 0;
while(i++ < 100000000)
{
    if (i instanceof Date)
    {
    }
    else if (i instanceof Date)
    {
    }
    else if (i instanceof Date)
    {
    }
    else if (i instanceof Date)
    {
    }
    else if (i instanceof Date)
    {
    }
    else if (i instanceof Date)
    {
    }
}
console.log(new Date().getTime() - n);

// prototype: 1839
// single instanceof: 1346 
// multiples instanceof: 4980 

Thanks,
Cheers.

@kumavis
Copy link
Contributor

kumavis commented Mar 14, 2015

I don't understand the conclusion

@sunag
Copy link
Collaborator Author

sunag commented Mar 25, 2015

Hi @kumavis .

This technique can you use proxy functions or simply replace them by other no need to modify the source code.

A example like this http://sunag.github.io/Oimo.js-Lab/test_bvh.html where replaces the functions flatten bone matrices native for a custom, such things can be done as a plugin since it uses prototype, besides the hierarchy based on prototype or similar be father of object-oriented language.

Cheers.

@thure
Copy link

thure commented Apr 8, 2015

+1 – discussed in this comment of #5886

@kumavis
Copy link
Contributor

kumavis commented Apr 8, 2015

@sunag thanks for the suggestion, I think its appropriate to fix the way this is being handled in the source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants