Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Friday, August 14, 2015

Heisenberg-effect while debugging JavaScript code in Chrome

I had a hard time finding a bug today in some AngularJS code and I think I got peopled by the Chrome JavaScript engine ( V8 right? ). I think it doesn't load variables into memory when it knows it's not going to use it. 

Check out my question here if you're interested on StackOverflow. I was trying to access that $http variable which is being injected into the controller, from the doSubmit function. However - I just had a debugger statement in that function and no direct call to $http. This gave me '$http not defined' errors all the time - so I thought I was doing things wrong with the controllerAs syntax or variable injection.

However, loosly interpreting the Heisenberg principle as 'measuring the system influences the system', turned out to be the case. The console.log() call influenced the system. Because of that line - the $http was not optimized away anymore and it does work if you keep that line.


Interesting stuff. The .NET counterpart of these kind of problems are Debug and Release builds. Check out the in-depth courses by Bart de Smet on C# if you're interested what's the difference is between these type of builds and why you could have NullRef exceptions in .NET code while running a Release build, that never occur in Debug mode.

Tuesday, April 21, 2015

Inspecting AngularJS page load performance using Chrome TimeLine / flame chart

I'm building a couple of prototypes for a page that needs to display around 2000 items in a grid of 3 items with 600+ rows. What we're looking to achieve is a quick page load time, but also no hassle while scrolling.
The case I'm investigating is a little out of the ordinary, in the sense that I assume that I already have the large object in memory. It's not like a more normal case where I send out paged AJAX calls to some REST server.

So, I've created a couple of AngularJS prototypes, which I've analysed with the Chrome Dev tools - the TimeLine tool to be exact. Here are some of my results.

1. Simplest form: load all data into a scope variable
OK, so I started out with the simplest form where I load the 2000 item array into a scope var, and display it using an ng-repeat. Let's see what that does in chrome dev tools:

You get this by opening up dev tools, going to TimeLine - hitting record and reloading the page. After clicking stop - you see this awesome graphical representation of the page load events + nested function calls ( flames ).

The DOMContentLoaded event fires and the AngularJS controller needs 200ms to render everything. Note the green line under the flame chart that shows the number of nodes in the DOM.



2. Infinite scroll.
Then I got an infinite scroll module for AngularJS which I applied. OnLoad I load 9 elements into the scope variable and every time the page event fires, I add another 9.


Page load time drops to 30ms (!) that's awesome. The little flames on the right are the scroll events. They take up about 3-5ms. each. This would be a lot more if I was making AJAX calls obviously,



However, I'm not sure this will give a nice scrolling experience to the user, especially on tablets. That's why I moved to option 3.

3. Seed loading + background loading of the rest
In this prototype, I load enough items to fill up the screen and return from the controller contstructor. Then after a timeout ( 100ms ), I append the rest of the data.


As you can see - a 30ms flame ( so as fast as the infinite scroll solution ) which loads 9 items into the viewmodel and handles the binding. Then after 100ms, I append the rest of the items ( approx 150 ms ). In the browser you see the DOM being built op very quickly and do not notice the rest of the items being loaded. The only thing you can see is the scrollbar jump up after 100ms, when the rest of the items are bound to the viewmodel and generated as HTML.


Right now - the last approach seems the most promising: very quick page load time and 'normal' scroll behavior.

Friday, April 17, 2015

Very interesting ( and long ) PluralSight course: Advanced JavaScript

I feel I still don't know as much about JavaScript as I would like. Honestly, the `new` and `this` keywords are still a bit wonky in my mind. Good thing Kyle Simpson has a great 6 hour video on Advanced Javascript and takes A LOT of time explaining function expressions, function keywords, this, new etc etc. 


Go check it out: http://www.pluralsight.com/courses/advanced-javascript or check out his FREE ebook series 'You don't know JavaScript'.

Thursday, March 19, 2015

Oops: Javascript: wrapping sync calls in a promise to they can be chained with async calls

As it turns out, I wasn't completely right in my previous post: http://jiffie.blogspot.nl/2015/02/javascript-wrapping-sync-calls-in.html.

Apparently it's not necessary to write :

// And now we create a promise from this
// instruction so we can chain it
var deferred = $.Deferred();
deferred.resolve(instruction);
return deferred.promise();

Why? I hear you ask? Because of the Monad pattern.
What's a monad? Good question. Read the book this guy wrote on Stack Overflow - http://stackoverflow.com/a/28947696/896697 - where he takes my answer and explains why I shouldn't have written it that way ...


Friday, February 27, 2015

Javascript: wrapping sync calls in a promise to they can be chained with async calls

Another StackOverflow answer that was recieved well by the person who asked the question was my answer on this question: http://stackoverflow.com/questions/28418601/generating-ajax-request-dynamically-based-on-scenario/28465401#28465401.

Here's the question:


So he's basically doing a bunch of async calls mixed with synchronous operations / mappings. In order to get a nice clean call chain - I wrapped all the synchronous things in a promise wrapper, so that all promises could be nicely chained:




Friday, October 17, 2014

I need AngularJS service instances, not singletons

One of the apps we're working with has a bunch of WebApi controllers and a bunch of AngularJS controller talking to them.

So we've got the backend URLs like:
- /api/typeA
- /api/typeB
- /api/typeC
- /api/typeD
- /api/typeE

Yes, we could bundle all the types in one WebApi, but then the object would become quite big and the saving part would require more logic. So we've got separate controllers for every type.

Most controllers only implement GET and POST and we were discussing an elegant way to get that into our app. There were a couple of options:
- Create a Angular service for every WebApi controller
- Create one service that takes the URL with every call
- Do something else ...

AngularJS only provides singletons ( link ) but I would really love to create a service instance that talks to a specific WebApi controller. Therefore I've sent a POC over to my colleague in which I suggest to have Angular inject a factory ( which is a singleton ), which is then able to build instances for each of the WebAPI controllers.

Here's my fiddle: http://jsfiddle.net/jochenvanwylick/0qcwver5/1/


Sunday, March 16, 2014

Filtering properties being watched by $scope.$watch in AngularJS

For one of my projects I needed undo/redo functionality. We implemented this by using just saving snapshots of the object to $sessionStorage.

However - we needed an easy way of being notified of object changes. Enter $scope.$watch. This method allows you to watch object for changes.

However - this method does not 'deep watch' into child property values. Also, we needed a bit more control over which model changes would trigger the snapshot mechanism. We solved it by adding a 'hash' method that creates a has of all the object properties that I care about.

Here's what I mean: http://jsfiddle.net/dsr4Y/2/


Monday, March 3, 2014

http://instantclick.io/ - smart

InstantClick is a JavasScript library that pre-loads the link you are about to click on, using the HTML5 browser history API. So as soon as you start hovering over a button or link, the target page is being pre-loaded in the background. At the moment you actually click the link, the target can be shown instantly.

The demo for the library is the site itself. Have a look at the click demo and see your latency between hovering and clicking for yourself.


Saturday, March 1, 2014

NProgress - youtube like progress bar

I came across this very elegant progress bar, quite like the youtube progress bar which updates when the browser push-state updates:


Sunday, February 2, 2014

Angular JS controller inheritance

Something I didn't know in AngularJS is that controllers can inherit from one another. My team member Pieter pointed this out to me and I think it's going to be very useful for us.

We're building a bunch of screens that have quite similar functionality ( start with a GET, POST on save etc ). In normal JavaScript, one can use prototypical inheritance of course, but that's not as evident with the Angular dependency injection.

However, since controllers can inherit from each other, it is! Check out this question on StackOverflow: http://stackoverflow.com/questions/18461263/can-an-angularjs-controller-inherit-from-another-contoller-in-the-same-module



Next week we'll be doing some refactoring!

Saturday, February 1, 2014

Zone.js

At the recent Angular Conference ng-conf, zone.js was presented. What I get from the video is that it's an execution context that lets you hook into ALL the function calls (including async ones) that are executed within the 'zone'.

This allows you to both instrument them properly ( get execution times ) but also possibly add some behavior before and after. Check it out at https://github.com/btford/zone.js/


Monday, January 6, 2014

The Durandal 2014 Kickstarter

Both Herding Code and .NET Rocks talkded with Rob Eisenberg about his Durandal 2014 kickstarter and it sounds VERY promising. He's very focused on modularity and implementing the lates browser native functionality. Go check it out and back him if you think it sounds right, he needs quite a bit more ...

http://www.kickstarter.com/projects/eisenbergeffect/durandal-2014

Monday, December 16, 2013

You are using web essentials ... right?

So this is my 2nd post in the 'you are using ... ... right?' series. There are certain things that I use that I thought everyone would be using by now, like ReSharper, Common.Logging and ... the Web Essentials extension for Visual Studio.


It has lots of tools for upping your productivity while developing web applications. One of the cool features is LESS compilation and also a bunch of CSS helpers to help you out with vendor specific tags.

Go and get it at: http://vswebessentials.com/


Wednesday, October 30, 2013

Humbling post to read: 'Did these guys just reinvent the Web?'

I read this post the other day: http://www.infoworld.com/t/application-development/did-these-guys-just-reinvent-the-web and it talks about http://famo.us/ . Seriously: check out that site. You know what, check it out on your phone!

Super fast JS animations. Done by some guy called Mark Lu, who came up with a prototype within a couple of days.

If you're not going to read the article, at least read this part:

Lu thought for a little while. "I think we may already have a solution to both of those concerns. To fix the rendering problem, the primary part we need to rewrite is the part of the renderer that creates and multiplies the matrices. And luckily for us, JavaScript is about 80 percent of the speed of compiled C for those specific types of operations." 

"That should give us a big advantage. How big?" 

Lu did some quick calculations. "Possibly an order of magnitude." 

Newcomb's jaw dropped. "That's ... really good. But how do we circumvent the browser's renderer without a plug-in?" 

"We use the CSS3 primitive -webkit-transform: matrix3d, which lets us compute the composite matrix and skip the browser's renderer. No plug-in, no download, no hack. By appending this to each DIV, we can render the composite matrix and go straight to the GPU." 

"Um, that sounds like really hard stuff." 

"Not really. I'll have it done by tomorrow."

WHAT?! By tomorrow? You're kidding me! I'm going to need until tomorrow to understand what he said in the first place. Guess I still have a long way to go.

Monday, October 21, 2013

Geting code coverage with QUnit: Blanket.js

Yet another *.js -charm  this time blanket. It's a code coverage tool for your QUnit tests. It works like a charm and requires next-to-no effort.

BlanketJS

I've added the library to my QUnit tests that I'm running and it does at it says on the tin. Works like a charm. Go check it out at http://blanketjs.org/.


Below are some of the tests I'm running. By the way - I'm using the Ninja theme for QUnit: http://qunitjs.com/plugins/ - at the bottom.


Wednesday, October 2, 2013

Getting started with Yeoman

In my previous post I installed NodeJS on my ubuntu machine. Now let's see what I can do with this ..

How about playing around with Yeoman ? I've heard it on several podcasts so it's about time I tried it out. Now that I installed Node - there's a whole bunch of coole new stuff I can discover.

So let's get it first:

sudo npm install -g yo

That get's me Yeoman! so that's cool. Now let's get a generator:

sudo npm install -g generator-webapp

The -g option makes installs the package as a global package.
That's it! My ubuntu has the LAMP stack installed, so I just created a folder in /www and ran

yo webapp

Yeoman pulls everything down and sets up a complete web application with test suite and everything.



Let's head over to localhost/www/yeoman/app:




and /test:

Sunday, September 1, 2013

JavaScript weekend in videos

We're currently working on an AngularJS website which is really awesome to be doing. So, we're writing a bunch of JavaScript. I wanted to raise my JavaScript knowledge to a higher level so I've been watching some YouTube movies on Javascript this weekend.



Here are a few interesting ones I had a look at.

A 3 part series on JavaScript by Douglas Crockford:
- Part 1: Douglas Crockford: The JavaScript Programming Language
- Part 2: Douglas Crockford: An Inconvenient API - The Theory of the DOM
- Part 3: Douglas Crockford: Advanced JavaScript

A quick 'by example' explanation of closures
Video by Bob Tabor.

Javascript patterns
Which will then lead me to understanding Javascript Patterns - as described here: Learning JavaScript Design Patterns



Javascript Ninja level
Finally - there is this site: http://ejohn.org/apps/learn/ - a site by a guy named John Resig who knows a thing or two about JavaScript ... he created jQuery.

Thursday, April 4, 2013

CodeMirror: online code editor

I came across this sleek and well working online code editor called CodeMirror. I'm sometimes hacking pieces of JavaScript into our CMS system, using CKEditor. However, CodeMirror looks really nice and the way to go for me from now on. Check it out on http://codemirror.net/ . Check out the JavaScript sandbox here.



Friday, March 29, 2013

JavaScript debugging from SublimeText using Chrome

Debugging JavaScript just became a lot sexier! Just install the chrome JavaScript debugger package for Sublime text and get going!