Press "Enter" to skip to content

Fast or Flexible? – FunFunFunction #17


good Monday morning I’m MP J and this is

fun fun function in last Monday’s

episode we explored the JavaScript

prototype a lot of comments on this

asking how does this relate to factory

functions and factory functions is a

video I made a couple of weeks ago so

we’re going to spend this episode

thinking about when to use a factory

function and when do we want to use a

prototype and it basically comes down to

your performance needs vector functions

are more flexible and they are easier to

reason about but if you need extreme

performance the prototype might be

something that you want to look at since

there will be of course you will be

lowering how how easy it is to reason

about your code and how flexible it is

we’re also going to look at what do we

mean by extreme performance and skits

I think the factory functions is a great

default choice factory functions are so

flexible they allow you to create

objects in any way you want including

private properties and all that good

stuff secondly I think that factory

functions are really easy to understand

because I used so few concepts a factory

function is just a normal function that

creates an object as long as somebody

understands closures they can read your

code and understand since the factory

function is just dealing with object

literals and closures this is not a lot

of moving parts factory functions

doesn’t use the this keyword which means

that you don’t have the issue of this

changing value depending on context

which means that you don’t have to do

any messing around with bind to keep

that in context factory functions very

simple robust flexible easy to reason

about prototypes have less of all those

things but prototypes have great

performance before we talk about that

though I want to show you this week’s

discovery that doesn’t have anything to

do with programming at all and from this

kingdom builder we were

we’re having a boardgame night this

weekend and this is just an amazing game

I really really liked it it made it’s

made by Donald X much amber brahbrah and

he is is the guy that designed the

Dominion card game if you’ve seen it I

found out about Kingdom Builder through

the amazing YouTube show a tabletop I

will read them which you should watch

anyway objects made with prototypes are

created slightly faster and they use

slightly less memory so if you find

yourself in a situation where you need

to create tons and tons and tons of

objects it might be a good idea to

sacrifice the benefits of the factory

function and just go for creating

objects using the prototype instead but

when I say tons of objects I actually

mean tons of objects I actually don’t

mean that because they are non physical

objects living inside a computer and

don’t weigh anything let’s have a pop

quiz and evaluate what you have learned

so far if you are creating 10,000 items

per second should you use the prototype

or factory functions if you answered

prototypes you are wrong 10,000 items is

nothing we’re living in the future

computer is inside the cheapest mobile

phone and all the major JavaScript

engines are amazingly fast um I really

like this quote by Josh liebe vague or

often devs still approach performance of

Jay s code as if they are riding a horse

cart of the horse have long been

replaced with a fusion reactor now if

you have an app that creates 10,000

items per frame not for a second per

frame then you need prototypes then

we’re talking well maybe you need them

if you are creating 10,000 items break

in

you might indeed need to create objects

really fast but before you do you should

really ask yourself is the problem maybe

that I’m creating 10,000 items per frame

in the first place

the answer to that question might

actually be no you might need to create

that many objects but it is really

important that you attack the

performance characteristics of your

actual algorithm not nuances of it like

object creation that is a few thoughts

and when to use a prototype of when to

use a factory function Factory functions

are a little bit more robust it more

easy to reason about and more flexible

but if you need extreme performance

actually need it then the prototype is a

better choice it’s super important to

not search for the ultimate tool I know

I nag about this a lot on this channel

but it is really important Factory

functions and prototypes is such a great

example of a situation that you often

end up in as a programmer will you have

a a flexible tool that is very simple

and robust and you have a fast tool

which is not as flexible and robust and

it’s very important that you actually

only sacrifice the flexibility of this

tool if you actually need this speed

you’re going to need both of these tools

in your toolbox the flexible tool and

the fast tool but you should wait to

throw away your flexibility until you

know that you have to if you don’t know

about the performance characteristics of

your application go with the flexible

tool until you do if you try to predict

the future and say that yeah this this

part of my code that this is definitely

going to be slow I should use a fast

tool here if you do that you are not a

computer scientist you are you’re no

better than the person writing

horoscopes next to the funny pages use

the flexible tool until you find out

what parts of your app are actually slow

in real life and then and only then you

break out the master

I am mvj this is fun fun function until

next Monday morning stay curious you

Please follow and like us: