<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1089686288236636980</id><updated>2011-08-12T07:20:41.661-07:00</updated><category term='javascript this closure scope new'/><category term='websockets flash nodejs connect node-websocket-server web-socket-js'/><title type='text'>nodejsbot</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://nodejsbot.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1089686288236636980/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://nodejsbot.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Bradley</name><uri>http://www.blogger.com/profile/03062713177364563086</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>6</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1089686288236636980.post-3123267503575410694</id><published>2010-11-14T08:53:00.001-08:00</published><updated>2010-11-14T10:04:55.580-08:00</updated><title type='text'>Javascript Performance</title><content type='html'>&lt;span class="Apple-style-span"&gt;The Javascript community does not often need to concern itself with speed, but here it goes. I am going to cover: Runtime Code Generation, Language Oddities, Lies, and Memory Consumption. If you want to go over some more general topics such as caching values, I suggest Google as these topics are more than I can truly cover in a single blog post as it is.&lt;/span&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;b&gt;Runtime Code Generation:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;This is one of my favorite features if I know that I am going to run a piece of code a huge number of times but only need to read the parameters once. This one time parameter (or constant parameters) lets me spend some time compiling code. &lt;b&gt;NOTE:&lt;/b&gt; you should not abuse code generation and do it all the time.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span"&gt;How to use it:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;Function / eval are the only ways to do this in Javascript. They do not generate sub-optimal performance functions (see: &lt;a href="http://jsperf.com/function-vs-constructor-vs-eval"&gt;http://jsperf.com/function-vs-constructor-vs-eval&lt;/a&gt;). The functions generate code that is as fast as anything else. I would highly recommend using Function instead of eval. Using Function does not allow access to variables outside of your function except parameters and the "this" variable.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;Example:&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;A simple cache function for a CSS selector like language (only takes tags and ids, is buggy etc.)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;/span&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;function SelectorFunction(selector) {&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;  var src = String(selector).match(/\#?[^a-zA-Z_\-$0-9]/g) &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;  src.map(function(selector_item){&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;    return selector_item.charAt(0) === "."&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;      ? "child = document.getElementById(\"" +selector_item.slice(1)+ "\"); if(child.compareDocumentPosition(node) !== Node&lt;span class="Apple-style-span"&gt;.&lt;span class="Apple-style-span" style="color: rgb(37, 34, 29); white-space: pre; "&gt;DOCUMENT_POSITION_CONTAINED_BY&lt;/span&gt;&lt;/span&gt;) return undefined; node = child"&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;      : "node.getElementsByTagName(\"" +selector_item+ "\")"&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;  })&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;  return Function("node","var child = null;" + src.join(";"))&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/blockquote&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span"&gt;When to use it:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span"&gt;When you have time to spare (setup / idle time / etc.)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"&gt;You know you are going to be able to infer something from your parameters that lets you hard code or pre-compute values&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span"&gt;Pros:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span"&gt;If done right, your code will gain quite a bit of speed and help remove possible bottlenecks&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"&gt;Caching precomputed values means parsing them again wont occur and wont use up memory in the process&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span"&gt;Cons:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span"&gt;If abused, this will slow down your code&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"&gt;Cached values may not be wanted for some things (dynamic settings)&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;b&gt;Language Oddities&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;As a language Javascript has some odd performance characteristics.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;This / Closures:&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span"&gt;Some engines, v8 in particular, aggressively optimize the lookup operator. You cannot do that optimization on a this variable since it is an unknown at compile time for some other inferences.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"&gt;Closures are faster than the this variable lookup time in v8.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"&gt;Closures that completely hide another closure will skip the closure above it in a scope chain (except function arguments and the catch variable in v8, this cannot be done to escape a with statement)&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;Numbers:&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-size: medium; "&gt;&lt;span class="Apple-style-span"&gt;Javascript is terrible at bitwise operators, TERRIBLE. Especially if you start mixing them with numbers that are acting like floating point numbers (this is the default number system in Javascript, engines optimize when you swap into integral calculation to prevent multiple casts over and over again).&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;Operators:&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-size: medium; "&gt;&lt;span class="Apple-style-span"&gt;The in operator is slower than the lookup operator. Why? It checks the entire prototype chain. The lookup operator in most new engines only performs a single check.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"&gt;The if statement is slower than the ternary operator which is slower than the or operator for single expression results (http://jsperf.com/default-args-or-vs-if/2)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"&gt;Avoid the delete operator unless you have an extremely long lived object. V8 will have to do some hoodoo in the background if you use it to reconcile some lookup optimizations which will take time.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;Statements:&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span"&gt;The with statement is slow... I mean really slow. Slower than emulating it with the dot operator (which means something slightly different, but oh well). This is because it must check the entire prototype chain of the object (akin to the in operator), and because almost no engine optimizes this statement due to its lack of use.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"&gt;Switch statements are fast, but unless you are only using Strings if/else branches can be optimized better by most engines.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;b&gt;Lies&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;A lot of what I have read about Javascript meta-programming is a lie.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span"&gt;If statements will not be precomputed and cached. They however are often&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"&gt;Objects as hashes only work until a certain size limit. Then they start to slow down due to GC (13~ million properties in the current v8, but then again who gets that far?).&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"&gt;StringVariable + "". This is the devils work for performance. It creates a full copy due to .toString etc this cannot be optimized out.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;b&gt;Memory Consumption&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;A lot of people do things that seem innovative, but tend towards high memory consumption.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span"&gt;Buffering! Do not do this when you only are going to pump out the data at the end! (In some cases you do not know if you want to pump it out if there is an error).&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"&gt;Throwaway functions do take memory, don't abuse them, cache them if you can (well cache everything you can really).&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"&gt;Please don't save extra properties onto host objects. Host objects tend to have slightly different GC rules (in IE 6-8 you can leak when messing with dom and not cleaning up!).&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"&gt;Avoid globals! Globals will never be GC'ed just avoid them when you can (certainly if you are leaving an object that can grow and grow and then be unused).&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1089686288236636980-3123267503575410694?l=nodejsbot.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://nodejsbot.blogspot.com/feeds/3123267503575410694/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nodejsbot.blogspot.com/2010/11/javascript-performance.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1089686288236636980/posts/default/3123267503575410694'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1089686288236636980/posts/default/3123267503575410694'/><link rel='alternate' type='text/html' href='http://nodejsbot.blogspot.com/2010/11/javascript-performance.html' title='Javascript Performance'/><author><name>Bradley</name><uri>http://www.blogger.com/profile/03062713177364563086</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1089686288236636980.post-7096651873793812935</id><published>2010-11-08T12:37:00.000-08:00</published><updated>2010-11-08T13:02:32.580-08:00</updated><title type='text'>Javascript "Classes"</title><content type='html'>&lt;div&gt;I would like to take some time to talk about a common JS misconception. "Faux Classes make JS easier." AKA beginning OOP in Javascript.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;First off let me note:&lt;div&gt;"Javascript does not contain nor can it truly emulate multiple inheritance, interfaces, namespaced privileges, declared throwing, or namespaces" (email me if you want really stupidly in depth evidence and opinions on anything you consider false in that)&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Ok. But aren't those nice to have?&lt;/div&gt;&lt;div&gt;In many languages yes,&lt;b&gt; in JS, they are not needed. &lt;/b&gt;Most implementations of these are approximations, and offer less performance than their pure JS counterpart.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Why?&lt;/div&gt;&lt;div&gt;1. They are not built in, and the way JS was designed &lt;b&gt;run-time checking is going be costly / assume things / etc.&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;1. 1. Assuming things is fine! I want my classes!&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;No, you don't. Learn the language, understand what you are asking. Don't learn JQuery or Dojo or ExtJS ... learn Javascript (Ecmascript to be picky). Javascript is nicer than you think, don't make it esoteric by having a metalanguage (talk to me about C preprocessor if you want to argue this).&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;1. 2. In my time looking at the job market recently, I have noted many companies have taken the plunge and depend upon these systems... Makes getting a job hard, makes employing people harder, and in general means if there is a design problem with your system you chose for a problem, you are boned.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;2. Most of these are actually harder to maintain than their JS counterparts! I cannot stress this enough. If you cannot absolutely follow a system for 100% of your project, interacting with other systems will become painful, even interacting with normal JS can be painful!&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Fine, whatever, they are prettier to read.&lt;/div&gt;&lt;div&gt;&lt;b&gt;+1 for classes if done right.&lt;/b&gt; (Coffeescript, arguably not really JS)&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Ok, so they are pretty and I don't need to understand JS inheritance or prototypes.&lt;/div&gt;&lt;div&gt;&lt;b&gt;-1 for classes.&lt;/b&gt; You will need to learn these at some point or your application will slowly bloat.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Classes are more performant than prototypes!&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;u&gt;No&lt;/u&gt;&lt;/b&gt;. Long blog post coming in next couple days on performance in JS.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I cannot stress this next point enough, you do not have to work with foo.prototype for every property. &lt;b&gt;For my sake, for your sake, for our sake:&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;blockquote&gt;&lt;/blockquote&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;div&gt;function foo(){}&lt;/div&gt;&lt;div&gt;foo.prototype = {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;bar: function(){},&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;baz: function(){}&lt;/div&gt;&lt;div&gt;};&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;/blockquote&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;But how do I do inheritance?&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;div&gt;function bar() {}&lt;/div&gt;&lt;div&gt;bar.prototype = new foo();&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;/blockquote&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;bar will have all the default prototype values of a default constructed foo. do not mix this and the one above, however, you may consider making your extension into a function itself.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;div&gt;function give_bar_methods(proto) {&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;proto.shazam = function () {}&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;function bar() {}&lt;/div&gt;&lt;div&gt;bar.prototype = new foo();&lt;/div&gt;&lt;div&gt;give_bar_methods(bar.prototype);&lt;/div&gt;&lt;div&gt;//now all bars get shazam!&lt;/div&gt;&lt;/blockquote&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;prototypes the way they are in JS have an amazing ability &lt;b&gt;RUNTIME MODIFICATION. &lt;/b&gt;Oh yea, thats right. Its awesome.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;div&gt;var x = new bar();&lt;/div&gt;&lt;div&gt;bar.prototype.y = function() {return 6}&lt;/div&gt;&lt;div&gt;x.y() //6&lt;/div&gt;&lt;/blockquote&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This can be extremely useful with lazy loading and some runtime codegen.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1089686288236636980-7096651873793812935?l=nodejsbot.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://nodejsbot.blogspot.com/feeds/7096651873793812935/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nodejsbot.blogspot.com/2010/11/javascript-classes.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1089686288236636980/posts/default/7096651873793812935'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1089686288236636980/posts/default/7096651873793812935'/><link rel='alternate' type='text/html' href='http://nodejsbot.blogspot.com/2010/11/javascript-classes.html' title='Javascript &quot;Classes&quot;'/><author><name>Bradley</name><uri>http://www.blogger.com/profile/03062713177364563086</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1089686288236636980.post-7954213086588063878</id><published>2010-10-07T21:04:00.000-07:00</published><updated>2010-10-11T08:50:05.699-07:00</updated><title type='text'>Node's require()</title><content type='html'>&lt;blockquote&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;/blockquote&gt;Node has a require function that is not entirely the same as the CommonJS one. And I for one am glad. CommonJS has a very strict very unflexible require. So, I am here to write how &lt;b&gt;require&lt;/b&gt; works in node.&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;Modules imported by require are cached on&lt;b&gt; absolute file path&lt;/b&gt;&lt;/li&gt;&lt;li&gt;Modules imported by require are not reloaded when required again unless you mess with very low level bits of node&lt;/li&gt;&lt;li&gt;Modules have their own scope&lt;/li&gt;&lt;li&gt;Modules have 3 means of exporting variables&lt;/li&gt;&lt;ul&gt;&lt;li&gt;exports.x = 'y' - would &lt;b&gt;use the default Object&lt;/b&gt; for exporting provided by node and add an 'x' property with value 'y'. require of this module would gain require(__filename).x === 'y'&lt;/li&gt;&lt;li&gt;module.exports = [1,2,3] - would &lt;b&gt;override the default Object&lt;/b&gt; provided for exports for something else (generally a function or a different object).  This would &lt;b&gt;replace all the exports you have already done&lt;/b&gt; and would make the require export an array with 1,2, and 3 instead.&lt;/li&gt;&lt;li&gt;global - if you don't have a good reason, and i mean, damn good, &lt;b&gt;don't attach a property to global&lt;/b&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;b&gt;require.paths &lt;/b&gt;- contains all the paths to search for &lt;b&gt;non-absolute/non-relative&lt;/b&gt; requires&lt;/li&gt;&lt;li&gt;the correct way to add a path to require.paths is to use &lt;b&gt;require.paths.unshift(__dirname)&lt;/b&gt;&lt;/li&gt;&lt;li&gt;require.main will be true if you are in the main module for a node process&lt;/li&gt;&lt;li&gt;require is a &lt;b&gt;unique function for each module&lt;/b&gt;, passing it around between modules is ill advised&lt;/li&gt;&lt;li&gt;require checks for many things when picking what file to pull in&lt;/li&gt;&lt;/ul&gt;&lt;ol&gt;&lt;li&gt;File at absolute path&lt;/li&gt;&lt;li&gt;File at path+".js"&lt;/li&gt;&lt;li&gt;Directory at path with "index.js"&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1089686288236636980-7954213086588063878?l=nodejsbot.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://nodejsbot.blogspot.com/feeds/7954213086588063878/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nodejsbot.blogspot.com/2010/10/nodes-require.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1089686288236636980/posts/default/7954213086588063878'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1089686288236636980/posts/default/7954213086588063878'/><link rel='alternate' type='text/html' href='http://nodejsbot.blogspot.com/2010/10/nodes-require.html' title='Node&apos;s require()'/><author><name>Bradley</name><uri>http://www.blogger.com/profile/03062713177364563086</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1089686288236636980.post-5904843193077864690</id><published>2010-09-16T19:38:00.000-07:00</published><updated>2010-09-19T20:26:49.648-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='javascript this closure scope new'/><title type='text'>Javascript Primer</title><content type='html'>Many people are learning JS when learning node here is a primer on what to learn outside of syntax. Learn them well...&lt;div&gt;&lt;br /&gt;&lt;div&gt;&lt;b&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Variables:&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;When you reference a variable in JS it can be a reference type &lt;b&gt;(Object, Array, Function)&lt;/b&gt; or a value type &lt;b&gt;(String, Number/NaN/Infinty, Boolean, null, undefined)&lt;/b&gt;. The variable name itself is typeless and can be considered a reference rather than any sort of direct relation to the data it holds. &lt;b&gt;The important difference is how data is copied when assigned to a new variable.&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Reference types&lt;/b&gt; may exist in multiple places at once being modified in place affecting all places. &lt;b&gt;Assignment is not a copy operation.&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Value types&lt;/b&gt; exist as they are and may not be modified in place. This means you make new copies of them for every modification you perform. &lt;b&gt;Assignment is a copy operation on the reference to an immutable value.&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;ALL operations except lookup '.' and invocation'(...)' attempt modification that ensures copying. Lookup and invocation operations do not modify in place (although the actions of invocation may change what the variable reference holds as its data).&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;b&gt;Scope:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Variables in javascript are given a scope that they exist in any operation in that scope will retrieve the variable with the name referenced&lt;b&gt; regardless of where / how many times you declare it&lt;/b&gt;. New scopes are created in only 1 situation inside of a &lt;b&gt;function&lt;/b&gt;. JS scopes are not based upon {}s like some languages.&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;ie.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;div&gt;function foo() {&lt;/div&gt;&lt;div&gt;  var i = 0&lt;/div&gt;&lt;div&gt;  if(i === 0) {&lt;/div&gt;&lt;div&gt;    var i = 1&lt;/div&gt;&lt;div&gt;  }&lt;/div&gt;&lt;div&gt;  return i&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;/blockquote&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;there are not 2 variables 'i' there is only one, i will equal 1 when it is returned, because there is only one scope. &lt;b&gt;do not attempt to use {} for a new scope!&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;*Function arguments exist in local scope&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;*With statements create a pseudo scope:&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;inside of a with statement any property of the argument is available&lt;b&gt; as if &lt;/b&gt;it was in a scope above the current one.&lt;b&gt; it does not create a new scope&lt;/b&gt; however.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;*Catch statements provide a temporary variable:&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;inside of a catch statement the argument reference is available, but it is &lt;b&gt;not assigned to the variable reference in the current scope&lt;/b&gt;. &lt;b&gt;it does not make a new scope ,&lt;/b&gt; but it may hide a variable in the current scope temporarily&lt;b&gt;.&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Closures:&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Remember scope? Well in JS all scopes above (outside of but not next to) your scope will be tried for when resolving a reference. This lets you pass around variables in interesting ways since functions can be created inside of a given scope. This detachment of scope is called a closure. Even &lt;b&gt;if a function leaves its own scope by being referenced elsewhere it retains its original scope&lt;/b&gt;!&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;ie.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;var function_array = []&lt;/div&gt;&lt;div&gt;for(var i=0;i&amp;lt;50;i++){&lt;div&gt;  function_array[i] = function() {return i}&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;all functions in function_array return 50. When they execute they retain their scope, and since for does not make a new one, they share i.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;b&gt;&lt;i&gt;Prototypes:&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;The actual knowledge&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;Javascript is a prototypal language. You make what you want the default object to look and act like and you go from there. However, JS does give you something that many prototypal languages do not, constructors.&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;In JS Functions are how we create objects. All you do is new foo(...) means :&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;create an object that has default values of foo.prototype &lt;/li&gt;&lt;li&gt;give it a prototype chain that holds foo.prototype's prototype chain&lt;/li&gt;&lt;li&gt;run the following statements with the&lt;b&gt; 'this' variable temporarily set&lt;/b&gt; to the object being created&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div&gt;Since prototype properties are default values that are applied before the constructor executes, &lt;b&gt;do not set up your prototype inside the constructor &lt;/b&gt;(unless you are doing something very very odd). Set it up outside the constructor, or else you will be redeclaring your defaults many times and since it is a shallow copy, &lt;b&gt;possibly even wreaking havoc if you do so.&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;*The instanceof operator&lt;/b&gt;&lt;/div&gt;&lt;div&gt;If a function changes its prototype by assignment directly (foo.prototype = {}) its prototype chain is lost and instanceof will fail where it would have worked previously.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-style: normal; font-weight: normal; font-size: 16px; "&gt;&lt;div&gt;In JS there are many arguments of whether classes exist or not (&lt;a href="http://en.wikipedia.org/wiki/Class_(computer_science)"&gt;Wikipedia&lt;/a&gt;).&lt;br /&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;The argument that classes exist&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;Javascript allows the creation of Object that may inherit properties from other objects in a chain. That by definition is the smalled type that may be associated in a JS execution. They are neither Object's in the sense that they rely on inheritance and neither types due having instance methods and properties.&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;The argument that classes do not exist&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;Classes imply things about languages after years of use to some of us. All of these are &lt;b&gt;subjective&lt;/b&gt; but an argument about classes as considered from a &lt;b&gt;per language implementation basis&lt;/b&gt;:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;Inheritance from multiple sources (interfaces and superclasses, or multiple inheritance). Classes&lt;/li&gt;&lt;li&gt;Access privileges: namely a distinction of protect and private, friendly and not friendly&lt;/li&gt;&lt;li&gt;Shared privileges: an ability to access private data of an instance of the same Class&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;Javascript does not provide these.&lt;/div&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;i&gt;This&lt;/i&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;The 'this' keyword in JS is &lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: medium; "&gt;something  that is &lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: medium; "&gt;&lt;b&gt;not derived from scope nor prototype. It is determined from &lt;u&gt;how&lt;/u&gt; the function was called. &lt;/b&gt;It can be changed on any function invocation by using foo.call(newthis,...) or foo.apply(newthis,...). If you wish to hold on to your reference (generally in a constructor) save the value of this to a variable (var self = this). The this keyword is set with the following rules:&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: medium; "&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;if calling the property of an object after using lookup (.) it is the object before the lookup operator&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;if calling without any lookup it is the global object&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;if calling with .call or .apply it is the first argument of those functions&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;b&gt;The value of this cannot be determined at any time except during the new operator&lt;/b&gt;, however,&lt;b&gt; your function may not be using the new operator and may then be pointing at another object.&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1089686288236636980-5904843193077864690?l=nodejsbot.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://nodejsbot.blogspot.com/feeds/5904843193077864690/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nodejsbot.blogspot.com/2010/09/javascript-primer.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1089686288236636980/posts/default/5904843193077864690'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1089686288236636980/posts/default/5904843193077864690'/><link rel='alternate' type='text/html' href='http://nodejsbot.blogspot.com/2010/09/javascript-primer.html' title='Javascript Primer'/><author><name>Bradley</name><uri>http://www.blogger.com/profile/03062713177364563086</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1089686288236636980.post-344279785415072216</id><published>2010-09-08T10:47:00.000-07:00</published><updated>2010-09-08T13:54:21.687-07:00</updated><title type='text'>RingoJS and Node.js</title><content type='html'>&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;RingoJS is an alternative to Node.js which has some promise. In this post I would like to compare what I consider the strengths of both and weaknesses of both. After that we can explain them in more depth.&lt;/span&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span"  style="font-size:x-large;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Weaknesses:&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:large;"&gt;&lt;b&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:large;"&gt;&lt;b&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Node.js&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;You may not have compile privileges for some C++ addons&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Node on Windows is tricky to deal with (though other operating systems are gaining some support with palm adding Node.js to its phones, Joyent running on solaris [no.de])&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Non-block style coding, flow of control can be unfamiliar to people used to synchronous or thread based programming&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Lack of legacy support, modules for XYZ may not exist&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Fast moving changes, most people are sticking to HEAD&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:large;"&gt;&lt;b&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;RingoJS&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Java running Javascript is slower than on C/C++&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Lack of use of Javascript's strengths in its api&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;High level api doesn't resemble coding style of browser&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Addons aren't really documented&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:x-large;"&gt;&lt;b&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Strengths:&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:large;"&gt;&lt;b&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Node.js&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;Asynchronous api allows evented programming like in a browser&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;Single threaded means avoidance of many synchronization issues&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;V8 is fast and the api is clean (google v8.h if you need help, almost always has the answer)&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;C++ addons mean you can do almost anything with them&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;API uses callbacks which exploits Javascript's closures to make your life easier&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;Low level api allows complex features to occur with relative ease compared to most apis&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;Evented systems scale stupid well compared to threaded (context change + memory overhead + locking, i wont go into more detail if you comment)&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;File descriptor passing is AMAZING&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span"  style="font-size:large;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;RingoJS&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Java addons! You can reuse all that old code&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;JS 1.8 features let you do more scripting goodness&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Can run multiple instances inside a single process (you can in v8, but its so hard lets just say you can't)&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Synchronous code means you don't have to figure out callbacks when debugging&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Asynchronous javascript code can block if needed using sync()&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;It runs on the JVM, aka, Google App Engine, your grandmothers computer, your phone, anything?&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Database support through Java, supported, tested, niiiiice~&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Desktop applications have a GUI/Multimedia/Input library to pull from Java&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Follows all the CommonJS stuff which is well argued about (I personally think that API is terrible when you could be using language features *first class functions / closures*)&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:x-large;"&gt;&lt;b&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Explanation&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:large;"&gt;&lt;b&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Coding Style&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Coroutines and Synchronous programming encourage what I like to call "block coding". People are going to be upset on both sides for this argument. Block coding (aka coding w/o first class functions) is a tradition. It has an advantage that it has been done forever by most coders. It has the disadvantage that it doesn't work well with events and it tends to lead to walls of text because there is no need to separate out logic for what should happen after x, because y is just after x, always. Some people are trying to bring coroutines to v8 and as such this is applicable to both.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;This comes with terrible and brilliant things (all of these are both good and bad). &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;The code can just be scanned without knowing whats going on behind the scenes and you know when things happened on a timeline.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;You dont need to separate out logic for when things happen&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Threads must be synchronized if multiple flows of control exist for your program&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Most systems encouraging this lock up all things that can cause synchronization issues&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span"  style="font-size:large;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Legacy&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Java libraries cover vast regions of problems, and the standard library is vast as it is. Your team probably has java doing something somewhere. C libraries can be hooked into node with relative ease if you use that ABI instead.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:large;"&gt;&lt;b&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Addons&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;I bring this up because, Javascript can do a lot, but you need to be able to extend it sometimes. Java addons run inside of a VM which is nice and safe, while C++ lets you work with low level and even more libraries (the JNI can work around this from both sides so its a non-issue). Node encourages addons, while I couldn't find too much documentation on the proper way to deal with RingoJS (I assume its the same as with Rhino, but something would be nice).&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1089686288236636980-344279785415072216?l=nodejsbot.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://nodejsbot.blogspot.com/feeds/344279785415072216/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nodejsbot.blogspot.com/2010/09/ringojs-and-nodejs.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1089686288236636980/posts/default/344279785415072216'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1089686288236636980/posts/default/344279785415072216'/><link rel='alternate' type='text/html' href='http://nodejsbot.blogspot.com/2010/09/ringojs-and-nodejs.html' title='RingoJS and Node.js'/><author><name>Bradley</name><uri>http://www.blogger.com/profile/03062713177364563086</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1089686288236636980.post-1637799728757873726</id><published>2010-07-25T10:50:00.000-07:00</published><updated>2010-07-25T12:09:25.577-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='websockets flash nodejs connect node-websocket-server web-socket-js'/><title type='text'>Flash Bridged Web Sockets in Node</title><content type='html'>Many people are talking about how to use websockets and where they are supported. Luckily we can use a Flash substitute in cased that they are not natively in Javascript. In this post I will create a small example of using this fallback technique. Our example will be a simple publish/submission chat.&lt;div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;For the example we are going to create we will need a few things:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;node &gt;= 0.1.100&lt;/li&gt;&lt;li&gt;&lt;a href="http://github.com/gimite/web-socket-js"&gt;http://github.com/gimite/web-socket-js&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://github.com/miksago/node-websocket-server"&gt;http://github.com/miksago/node-websocket-server&lt;/a&gt; (npm websocket-server)&lt;/li&gt;&lt;li&gt;&lt;a href="http://github.com/senchalabs/Connect/"&gt;http://github.com/senchalabs/Connect/&lt;/a&gt; (npm connect)&lt;/li&gt;&lt;li&gt;a port to serve files on (8000 in our example)&lt;/li&gt;&lt;li&gt;port 843 on the server&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Note:&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;Flash websockets will not be active until the SWF is loaded, either wait for an onload or test other ways to see if the WebSocket interface is ready.&lt;/li&gt;&lt;li&gt;IE 64-bit does not support Flash as of the time of this post.&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;b&gt;&lt;i&gt;Websocket Client:&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;b&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;web-socket-js has a sample.html file which will serve as our example of a simple publication/subscription service client. It requires &lt;b&gt;WebSocketMain.swf&lt;/b&gt; , &lt;b&gt;swfobject.js&lt;/b&gt; , &lt;b&gt;web_socket.js&lt;/b&gt; , and &lt;b&gt;FABridge.js &lt;/b&gt;. All the browser will have to do to get to this is direct to this page from HTTP:// (Flash Sockets will not work on FILE://)&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The only modifications to this file that we need to do are where the WebSocket(url) is going.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;If you move WebSocketMain.swf be sure to set WEB_SOCKET_SWF_LOCATION&lt;/li&gt;&lt;li&gt;You can remove WEB_SOCKET_DEBUG if you do not want messages in your console&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;b&gt;&lt;i&gt;Websocket Server:&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;Our server needs to do 3 things:&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;Serve the files we want to make up our client&lt;/li&gt;&lt;li&gt;Accept websocket connections&lt;/li&gt;&lt;li&gt;Serve a policy file using TCP (Not HTTP://!)&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;To see all of these in action download:&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;a href="http://github.com/nodejsbot/flash-websocket-example"&gt;http://github.com/nodejsbot/flash-websocket-example&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;span class="Apple-style-span" style="font-size: 16px; "&gt;&lt;div&gt;&lt;div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1089686288236636980-1637799728757873726?l=nodejsbot.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://nodejsbot.blogspot.com/feeds/1637799728757873726/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nodejsbot.blogspot.com/2010/07/flash-bridged-web-sockets-in-node.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1089686288236636980/posts/default/1637799728757873726'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1089686288236636980/posts/default/1637799728757873726'/><link rel='alternate' type='text/html' href='http://nodejsbot.blogspot.com/2010/07/flash-bridged-web-sockets-in-node.html' title='Flash Bridged Web Sockets in Node'/><author><name>Bradley</name><uri>http://www.blogger.com/profile/03062713177364563086</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
