webmastering

Function overloading in JavaScript

I won’t talk about PHP today. Sorry PHPers. If you do some fron­tend job, you won’t be disap­po­in­ted. I’m assu­ming you know some basic Java­Script stuff like closu­res, proto­ty­ping and context swit­ching, because I’m going to do some cheating.

Our goal for today is to force Java­Script to invoke diffe­rent func­tions accor­ding to diffe­rent argu­ments number. To further compli­cate all of this, we’ll work on proto­ty­pes and check if over­lo­ading works on inhe­ri­ted objects.

I’ll use Coffe­eScript since it has nice, conden­sed syntax. You don’t need to write much JS boiler­plate like semi­co­lons and braces, what leads to about 30% less code in CS <-> JS compa­ri­son. Always remem­ber: Coffe­eScript is just Java­Script and you can simple paste it in Try Coffe­eScript’s section to see the JS compi­led result. Don’t worry, i’ll provide some hints in sensi­tive places.

Most impor­tant things for today are:

  • you can always check how many argu­ments func­tion has in its decla­ra­tion using length property,
  • you can always check how many argu­ments func­tion was invo­ked with by using arguments.length property.


A few ending thoughts:

  • augmen­ting methods have some perfor­mance penalty,
  • you’ll proba­bly compli­cate stuff when use over­lo­ading too much,
  • if you truly need that solu­tion in JS, you’re doing some­thing wrong :-)
webmastering

Using streams in multi process environment

Today is the last instal­l­ment about PHP stre­ams. This time we will borrow some images from GitHub octo­dex using remote stre­ams and stream copy­ing. To add some spice, we’re going to use PHP in paral­lel proces­sing. If you thought PHP is single process srcrip­ting langu­age, I will ruin your world, sorry ;-)

Of course in real world projects there is always a trade off between time and reso­ur­ces. Forking process is CPU expen­sive work for server. We’re assu­ming we have plenty of CPU cycles and RAM, but short on time. Take a look on code below:

It took 5 secs to fetch 127 images. The same thing run in single fore­ach loop takes about 2 minutes…

webmastering

Writing custom stream filters

Perhaps you used once or twice buil­tin zlib.* or mcrypt.* stream filters. Have you tried making your own stream filter? Maybe you didn’t need to. There are ways to accom­plish busi­ness needs without making hands dirty by not well docu­men­ted stuff like user filters. I need to know that topic like I said before, because of certification :-)

We’re going to make stream filter capa­ble of compu­ting hash check­sum from stream contents on the fly. Rich docu­men­ted code below: