advertisement

JavaScript tips - Unnest callbacks and method declarations

50 %
50 %
advertisement
Information about JavaScript tips - Unnest callbacks and method declarations
Technology

Published on March 6, 2014

Author: exponential_io

Source: slideshare.net

Description

JavaScript tips - Unnest callbacks and method declarations for improved readability and better performance
advertisement

JavaScript Tips Unnest Callbacks and Method Declarations to improve readability and performance Akbar S. Ahmed Founder, Exponential.io – Build better apps faster with less effort. Twitter: @exponential_io Web: www.Exponential.io

Agenda • Callbacks – Nested – Unnested • Method Declarations – Nested – Unnested Git: https://github.com/akbarahmed/unnest-callbacks-in-javascript

Response time is 290 ms. Nested Callback #!/usr/bin/env node var fs = require('fs'); fs.readFile('./file1.txt', function (err, file1Contents) { if (err) throw err; fs.readFile('./file2.txt', function (err, file2Contents) { if (err) throw err; var file3Contents = [file1Contents, file2Contents].join(''); fs.writeFile('file3.txt', file3Contents, function (err) { if (err) throw err; console.log('Concatenated file1.txt and file2.txt into file3.txt.'); }); }); });

Response time is 114 ms. Unnested Callback: Properties #!/usr/bin/env node var fs = require('fs'); fs.readFile('./file1.txt', readFile1); function readFile1(err, data) { if (err) throw err; writeFile3.file1Contents = data; fs.readFile('./file2.txt', readFile2); } function readFile2(err, data) { if (err) throw err; writeFile3.file2Contents = data; var file3Contents = [writeFile3.file1Contents, writeFile3.file2Contents].join(''); fs.writeFile('file3.txt', file3Contents, writeFile3); } …

Response time is 114 ms. Unnested Callback: File Globals #!/usr/bin/env node var fs = require('fs'); var file1Contents, file2Contents, file3Contents; fs.readFile('./file1.txt', readFile1); function readFile1(err, data) { if (err) throw err; file1Contents = data; fs.readFile('./file2.txt', readFile2); } function readFile2(err, data) { if (err) throw err; file2Contents = data; file3Contents = [file1Contents, file2Contents].join(''); fs.writeFile('file3.txt', file3Contents, writeFile3); } …

Unnested Callback Benefits • • • • Code is easier to read Flow of code is clear Better performance Named functions yield better error messages

Response time is 280 ns (that’s nanoseconds). Nested Method Declaration #!/usr/bin/env node function Cat(name) { this.getName = function () { return name; }; this.setName = function (n) { name = n; }; } var meows = new Cat('meows'); console.log('I am a cat named ' + meows.getName());

Response time is 7 ns (that’s nanoseconds). Unnested Method Declaration #!/usr/bin/env node function Cat(name) { this._name = name; } Cat.prototype.getName = function () { return this._name; }; Cat.prototype.setName = function (n) { this._name = n; }; var meows = new Cat('meows'); console.log('I am a cat named ' + meows.getName());

npm install -g exponential • Exponential.io alphas recently released • Your input would be greatly appreciated • Email: akbar@exponential.io • Twitter: @exponential_io

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

GitHub - akbarahmed/unnest-callbacks-in-javascript ...

unnest-callbacks-in-javascript - Example code on how to unnest callbacks for readability and performance improvements. ... Unnested method declarations.
Read more

BayNode - Akbar S. Ahmed - Unnest Callbacks for ...

... Unnest callbacks and method declarations for improved readability and ... JavaScript tips - Unnest callbacks and method declarations ...
Read more

Callbacks, promises, generators - asynchronous javascript ...

Share Callbacks, promises, generators - asynchronous javascript. ... ( Asynchronous JavaScript XML ) ... Unnest callbacks and method declarations.
Read more

added method declarations examples · akbarahmed/unnest ...

unnest-callbacks-in-javascript - Example code on how to unnest callbacks for readability and performance improvements
Read more

callbacks - Documents

Share callbacks. Embed. size(px) start ...
Read more

Interfaces, Callbacks, Delegates and Events - Documents

Share Interfaces, Callbacks, ... Delegates are objects that contain references to method that need to beinvoked instead of containing ...
Read more

Passing extra data to callbacks and scoping issues within ...

Passing Extra Data to Callbacks. ... For an alternative approach you may use a Javascript closure. Recommended Method. ... Hints and Tips;
Read more

Callbacks | LinkedIn

View 11588 Callbacks posts, presentations, experts, and more. Get the professional knowledge you need on LinkedIn. LinkedIn Home What is LinkedIn?
Read more

13 JavaScript Tips & Tricks // Josh Wright

13 JavaScript Tips & Tricks ... In JavaScript, ... > Cannot call method 'toString' of null (anonymous function) ...
Read more