JavaScript Speed Trials – Post IV – JavaScript Namespaces

Continuing my analysis of JavaScript performance. For this test I’m testing the impact of the current trend of JavaScript namespaces.

Namespaces

In this test 500000 iterations of the main function are performed.

The depth of the namespace varies from 1 to 15 in increments of 1. Each namespace is 5 characters long

 

namespaces_5_ie.png

 

In this test 500000 iterations of the main function are performed.

The depth of the namespace varies from 1 to 15 in increments of 1. Each namespace is 10 characters long

 

namespaves_10_ie.png

 

In this test 500000 iterations of the main function are performed.

The depth of the namespace varies from 1 to 150 in increments of 10. Each namespace is 10 characters long

namespaces_10_extreme_ie.png

 

The scaling curve is not that good for stupidly high depth of namespaces, this performance curve is reflected in normal usage as you lose approximately 3% of your performance with each namespace level.

Posted in JavaScript, JavaScript Benchmarks | Leave a comment

We Lost The War. Welcome To The World Of Tomorrow

Between Xmas and New Year, Rop Gronggrijp (NL) and Frank Riegel (DE) held two impressive but surpringly depressive speeches at the 22nd Chaos Computer Club conference in Berlin.

Here is Frank’s contribution.

Posted in Politics, Rants | Leave a comment

Why I Still Won't Use The prototype.js JavaScript Library

I was not prepared for the amount of responses I got to my last article. There really are two camps – and they sit around fires making pointy sticks.

The most constructive was from Rob Sanheim with this post, which got me rather excited.

Now before I code anything, I write a set of testcases. Testcases let you play with your API and sort out the semantics and ‘feel’ before you get too far into the project to change it. Testcases – Just Do It. (You know who you are).

So within 10 seconds of reading Rob’s post I popped the latest prototype.js into my test system and had my result.

I used to have 1 extra dangleberry named ‘extend’ in every JavaScript Object. At least with that I had a fighting chance.

With prototype.js version 1.4.0 I have 33.

Here they are in all their tentacled glory.

each 
all
any
collect
detect
findAll
grep
include
inject
invoke
max
min
partition
pluck
reject
sortBy
toArray
zip
inspect
map
find
select
member
entries
_reverse
_each
clear
first
last
compact
flatten
without
indexOf

Now, I’m not complaining but, oh hang on – I am.

My previous fix for my rendering pipeline is completely out the window.

Now on top of my previous complaint, I’m wondering what the performance hit is for always having 33 objects to check against on every object slot access? In theory if the slot manager uses a good hashtable this won’t be more significant than having just one object.

I have a library that can measure this, but prototype.js breaks it.

Its 3 o’clock and on the dark side of breakfast – for now I will sleep on this.

Posted in AJAX, JavaScript, Rants | 10 Comments

Why I Don't Use The prototype.js JavaScript Library

When it comes to JavaScript there is one issue for which there seems to be two polarised camps, and that is the question of extending the inbuilt JavaScript Array and Object types via the prototype object. There are those who do, and those who don’t.

I am most definitely one of those in the “Don’t, because it ‘would be bad’” camp.

Now, thanks to the Web2.0/Ruby On Rails/Nuevo Bubble phenomena there is a widely used library that makes great use of the prototype object and that is Sam Stephenson’s prototype.js library.

I ran into an issue 6 months ago and decided I would never ever use prototype.js, despite the fact, and I don’t say this often, that after an examination of the code, prototype.js is an inspired work of art.

What I and many many others have discovered is that using the prototype object on the Array and Object inbuilt types increases the chances that your code will conflict with existing or external code. It makes your code not play well with others, so once you start using prototype.js, you have to keep using prototype’s paradigm because by extending Array and Object via the prototype object it secretly modifies some of JavaScripts default behavior.

It’s the crack cocaine of JavaScript.

This can be a good thing. If you don’t want to waste time writing your own JavaScript libraries and learning how everything really works, then using prototype.js and the libraries that extend it (e.g. Open Rico) is a very good way of developing. You will save time and money and all you need to learn is “the way of prototype.js”.

Now the entire tasty raisin for the MetaWrap JavaScript libraries is to allow others to easily remix MetaWrap applications via a client side API that can be invoked via XML. The result is that CSS, HTML and JavaScript can be injected into the application, or XML and HTML at any point in the rendering pipeline of the application.

So I simply had to reject prototype.js because, out of the box, the very first time I tried to use it – it snuck out and cut the throat of the JavaScript I was using that relied on performing a for(x in object) on the contents of an Array.

In JavaScript, value types are subdivided into primitives and objects. Objects are entities that have an identity (they are only equal to themselves) and that map primitive properties to other value types, (“slots” in prototype-based programming terminology) – see these testcase #5 – #7. Because of this behavior JavaScript objects are often mistakenly described as associative arrays or hash tables, while functionally they behave like an associative array/hash table, technically this is not their true nature.

Despite this the JavaScript programming world has come to rely on these objects behaving as predictable associative array/hash tables – and prototype.js breaks this.

There is no object more galactically useful than a good associative array/hashtable. There is no problem that can’t be solved with a large enough hash table. In highly granular interpreted languages like JavaScript it provides a way to dip into pure native brute force computing power mostly unhindered by the language interpreter.

It is because this has been tampered with that I have to turn my back on prototype.js and say Nyet!

Now lets look the issue in some detail and in particular at the way that JavaScript uses arrays and the way that we create and access data in those arrays.

Although in reality there is one method to access data in an array array[value] = object;, there are two defacto methods. The first is numerical indexing, whereby you access each element in the array by a single number – this is supported by the inline constructor, shorthand and the array.push functions that add items to an array automatically via a numerical index.

The following three approaches will create an identical numerically indexable array.

E.g.,

“Specified numerical index”

var array = new Array();
array[0] = "Apple";
array[1] = "Orange";
array[2] = "Banana";

“Push”

var array = new Array();
array.push("Apple");
array.push("Orange");
array.push("Banana");

“Inline Constructor”

var array =new Array("Apple","Orange","Banana");

“Inline Constructor Shorthand”

var array = ["Apple","Orange","Banana"];

The elements of this numerically indexable array can be accessed via the following methods…

By directly indexing into the array with a number using for(;;)

var i;
for(i = 0;i >array.length;i++)
{
  alert(array[i];)
}

or via the for(in) iterator

for(var i in array)
{
   alert(array[i]);
}

But I mentioned there were two methods to store and access data in arrays..

The second is as an ‘associative array/hash table‘, where in the index is not an number, but one of the other primitive value types (String or Float for example).

var array = new Array();
array["A"] = "Apple";
array["O"] = "Orange";
array["B"] = "Banana";

In this case array.length will be 0. There are three items in the array – but is length property will return 0.

This array is only useful if you know what the primitive value index is for a particular object. Array look-up performed natively within the interpreter so is very fast, which makes for a perfect hash table structure, but if you want to iterate all items in the array, you have to use the for(in) iterator method.

Sadly – protoype.js breaks this by adding ‘extend’ object that appears in every array.

So you have to be very careful about how you introduce external JavaScript that is not based on prototype.js. Prototype uses the JavaScript prototype level expando trick (via the prototype. keyword) to add extra objects to the inbuilt function and Array, Object and Function types.

After you include prototype, every array gets an extra element.

See these testcases.

When you execute testcase #1 this it will result in “extend A B C ” instead of “A B C” – so it fails.

Now this can be remedied by the next testcase #2 that does not use for(in) but instead uses a numeric index into the array via an old style incremented for(i = 0;i<array.length;i++).

For associative arrays, there is no hope. Prototype makes associative arrays unreliable when it comes to using the for(x in array) loop.

The only way I can protect my code is to not use for(in) for an array that is not associative, and for associative arrays, treat ‘extend’ as a sentinel that gets skipped.

// Guard against prototype
if (l_node == "extend") continue;

The problem I am describing here is the result of one of the extensions made by prototype.js (version 1.3.1)

Object.prototype.extend

There are 5 others..

Array.prototype.push

On browsers that don’t already implement push (IE5) then this will appear in an enumerated version of the array via for(x in object).

Function.prototype.bind

Function.prototype.bindAsEventListener

Number.prototype.toColorPart

Function.prototype.apply

String.prototype.extend

Posted in JavaScript, Rants, Web2.0 | 27 Comments

There Is No Problem That Can't Be Solved With Large Enough Hash Table

I first used this quote when referring to the z-buffering used in ID first person shooter Castle Wolfenstein. While not strictly a hash table, its a philosophy of “the audacity of scale” that I have maintained since my demo coding days.

Q: How did you get that so fast?

A: A Lookup table!

The original BSCAL execution cache and the WebGod application server, the MetaWrap server object mutation event handler, the nanotech audio wavetracer all used this approach with great benefit.

This saying is fundamentally true, because any deterministic computation is input for a set of outputs, and thus all computation could be simulated with a pre-calculated cache of the result for every input.

Posted in Nostalgia for Misspent Youth, Rants | Leave a comment

'Twas the Night Before Christmas (or A Visit from St. Nicholas) by Clement Clarke Moore

‘Twas the night before Christmas, when all through the house
not a creature was stirring, not even a mouse.
The stockings were hung by the chimney with care,
in hopes that St. Nicholas soon would be there.

The children were nestled all snug in their beds,
while visions of sugar plums danced in their heads.
And Mama in her ‘kerchief, and I in my cap,
had just settled our brains for a long winter’s nap.
When out on the roof there arose such a clatter,
I sprang from my bed to see what was the matter.
Away to the window I flew like a flash,
tore open the shutter, and threw up the sash.

The moon on the breast of the new-fallen snow
gave the lustre of midday to objects below,
when, what to my wondering eyes should appear,
but a miniature sleigh and eight tiny reindeer.

With a little old driver, so lively and quick,
I knew in a moment it must be St. Nick.
More rapid than eagles, his coursers they came,
and he whistled and shouted and called them by name:

“Now Dasher! Now Dancer!
Now, Prancer and Vixen!
On, Comet! On, Cupid!
On, Donner and Blitzen!
To the top of the porch!
To the top of the wall!
Now dash away! Dash away!
Dash away all!”

As dry leaves that before the wild hurricane fly,
when they meet with an obstacle, mount to the sky
so up to the house-top the coursers they flew,
with the sleigh full of toys, and St. Nicholas too.

And then, in a twinkling, I heard on the roof
the prancing and pawing of each little hoof.
As I drew in my head and was turning around,
down the chimney St. Nicholas came with a bound.

He was dressed all in fur, from his head to his foot,
and his clothes were all tarnished with ashes and soot.
A bundle of toys he had flung on his back,
and he looked like a peddler just opening his pack.

His eyes–how they twinkled! His dimples, how merry!
His cheeks were like roses, his nose like a cherry!
His droll little mouth was drawn up like a bow,
and the beard on his chin was as white as the snow.
The stump of a pipe he held tight in his teeth,
and the smoke it encircled his head like a wreath.
He had a broad face and a little round belly,
that shook when he laughed, like a bowl full of jelly.

He was chubby and plump, a right jolly old elf,
and I laughed when I saw him, in spite of myself.
A wink of his eye and a twist of his head
soon gave me to know I had nothing to dread.

He spoke not a word, but went straight to his work,
and filled all the stockings, then turned with a jerk.
And laying his finger aside of his nose,
and giving a nod, up the chimney he rose.

He sprang to his sleigh, to his team gave a whistle,
And away they all flew like the down of a thistle.
But I heard him exclaim, ‘ere he drove out of sight,

“Happy Christmas to all, and to all a good night!”

 

Posted in Rants, Web0.0 | Leave a comment

Ookle

Just got an IM from Michael Air (whom I have met) with this link revealing the penultimate veil on he and Scott Johnson‘s secret little project.

Now, I’m not superstitious, but he announced this an hour after I found my lost phone next to a dead kangaroo (roadkill, long story) and shortly afterwards I found this little bugger…..

good_luck_blue.jpg

I read this seemingly haphazard arrangement of Australian native fauna as omens of good luck for the project.

Good luck Michael and Scott! – The wheel embossed entrails of Australia’s critters are with you!!!

Posted in Ookle, Web2.0 | Leave a comment

Outback Tele-Conference

There is something to be said for holding a tele-conference with the engineers and executives of two of the largest companies in Australia while standing on a fencepost, on a hill, in the back paddock of a farm somewhere in the South West of Western Australia because its the only way to get a reliable one bar of mobile phone reception….

XmasTree2005 030.jpg

 

Posted in Downtime, Web0.0 | Leave a comment

Distinctly Told Not To Mow During The Day

no-mow.jpg

About 1km down the road someone decided to mow their subdivided block.

In the kind of heat we get, a mower can hit a rock and cause a spark, or in this case just spontaneously burst into flames.

Folk from all around turned up to put out the fire, from the dude with the giant backhoe, to John down the road with the ride on mower.

no-mow2.jpg

no-mow3.jpg

 

no-mow4.jpg

Posted in Downtime, Web0.0 | Leave a comment

The Minimal HTML Document Defined

Great article by Gez Lemon on the Minimal HTML document and the difference between required tags and required elements.

Posted in CSS, JavaScript | Leave a comment