SlideShare a Scribd company logo
Effective JavaScript
Effective JavaScript
          Justin Tulloss
     Sr. Software Engineer
         April 24, 2010
Overview

• JavaScript language properties
• Effective closures
• Effective objects
• Interacting with the DOM
• ECMAScript 5
• v8 optimizations
Prototypes
Assigning to a Prototype
Altering a Prototype
Altering a Prototype Faster
Deriving an Object from Another
Object
Deriving an Object with
Object.create
Scope Resolution
What Is in the Resolution Chain?
Functions dictate scope (with a few exceptions)



• Local variables
• Arguments
• Enclosing function scopes
• With Statement
• Global
Each Step Is Like a Property Access




locals.arguments.scope1Locals.scope1Args.globals
Example
Don’t Use with
Effective Closures
The Power of Closures
With great power comes great responsibility



• Reduce typing
• Make code cleaner
• Provide some privacy
• Sometimes they make things fast
• Magic (AKA functional)
Building Objects with Closures
Same Thing Works for Constructors
Avoiding bind in Asynchronous
Operations
Avoiding Unnecessary Methods
Memoization
Effective Objects
Using Objects Effectively

• First, do what makes sense—don’t over-optimize
• Every property access is slow
• Breadth not depth
• Use object literals
• Make shortcuts
• Inspectable
Use Object Literals


 Bad




 Good
Interacting with
    the DOM
Don’t
DOM Tips

• Watch out for closures referencing nodes
  • These can leak
• Reference nodes anyway
  • Using a reference is much faster than looking up a node
• innerHTML is generally faster than the DOM API
ECMAScript 5
Exciting New Features

• Function.prototype.bind
• Object.create
• Getters/setters
• String.prototype.trim
Getters and Setters
v8 Optimizations
v8 Optimizations

• Don’t use delete
• Use object literals
• Avoid altering objects after creating them
• Don’t use eval
If it’s hard in C,
    it’s slow in
Q &A
Effective JavaScript

More Related Content

PDF
Promoter – A Python Project for Replicating a JSS via the API
PDF
10 Things you should know about Ruby
PPTX
Serverless testing-serverless-sydney-20181018
PDF
RubyConf China 2015 - Rails off assets pipeline
PPTX
"GitHub com for lazy in cloud project management" by Sergejs Matancevs from S...
PPTX
Introduction to Reactjs
PDF
A Documentation Crash Course, LinuxCon 2016
PPTX
Eurosport's Kodakademi #2
Promoter – A Python Project for Replicating a JSS via the API
10 Things you should know about Ruby
Serverless testing-serverless-sydney-20181018
RubyConf China 2015 - Rails off assets pipeline
"GitHub com for lazy in cloud project management" by Sergejs Matancevs from S...
Introduction to Reactjs
A Documentation Crash Course, LinuxCon 2016
Eurosport's Kodakademi #2

What's hot (10)

PPTX
Intro to TypeScript, HTML5DevConf Oct 2013
PDF
Php : Why and When!
PDF
AWS Lambda Function with Kotlin
PDF
Javantura v4 - Java or Scala – Web development with Playframework 2.5.x - Kre...
PDF
Truly madly deeply parallel ruby applications
PDF
Beyond The Rails Way
PDF
The Holy Grail of continuous delivery in distributed teams environment
PPTX
.Net Core 3.0. What’s inside? Павло Голубович
PDF
From Rails legacy to DDD - Pivorak, Lviv
PPTX
MySQL-Mixer-Livenation
Intro to TypeScript, HTML5DevConf Oct 2013
Php : Why and When!
AWS Lambda Function with Kotlin
Javantura v4 - Java or Scala – Web development with Playframework 2.5.x - Kre...
Truly madly deeply parallel ruby applications
Beyond The Rails Way
The Holy Grail of continuous delivery in distributed teams environment
.Net Core 3.0. What’s inside? Павло Голубович
From Rails legacy to DDD - Pivorak, Lviv
MySQL-Mixer-Livenation
Ad

Viewers also liked (20)

PPTX
BlueVia SDK for .NET Overview
PDF
Head First Java Chapter 3
PPTX
C# programming language
PDF
Html5 overview
PPT
.Net overview
PDF
Modern JavaScript Programming
PPT
Basic knowledge of cyber security
PPT
cpuk10745
PPTX
Html 5 tutorial - By Bally Chohan
PDF
Expert selenium with core java
PPTX
Core java over view basics introduction by quontra solutions
PPTX
HTML5: An Overview
PPT
Html5 Overview
PPTX
Low Latency Networking on IOS and Android over Cloud by Oguz Bastemur
PDF
ASP.NET MVC - Javascript Overview
PPTX
An overview of JavaScript
PPT
High Level Broadband Overview
PPT
Verizon Overview Broadband during DigiWorld Summit 2011
PPT
Ado.net
BlueVia SDK for .NET Overview
Head First Java Chapter 3
C# programming language
Html5 overview
.Net overview
Modern JavaScript Programming
Basic knowledge of cyber security
cpuk10745
Html 5 tutorial - By Bally Chohan
Expert selenium with core java
Core java over view basics introduction by quontra solutions
HTML5: An Overview
Html5 Overview
Low Latency Networking on IOS and Android over Cloud by Oguz Bastemur
ASP.NET MVC - Javascript Overview
An overview of JavaScript
High Level Broadband Overview
Verizon Overview Broadband during DigiWorld Summit 2011
Ado.net
Ad

Similar to Effective JavaScript (20)

PPTX
Javascriptinobject orientedway-090512225827-phpapp02
PPTX
Object oriented java script
PPTX
Awesomeness of JavaScript…almost
PDF
JavaScript Basics and Best Practices - CC FE & UX
PPT
Advanced JavaScript
PPTX
Javascript Best Practices and Intro to Titanium
PDF
[2015/2016] JavaScript
PPTX
ECMAScript 2015
PPTX
Unit-3.pptx node js ppt documents semester-5
PPTX
Java script Techniques Part I
PPTX
Java scriptforjavadev part1
PDF
Javascript Performance
PDF
Javascript closures
 
PPTX
All of javascript
PDF
Solid JavaScript Coding
PPTX
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
PDF
Run-time of Node.js : V8 JavaScript Engine
KEY
Exciting JavaScript - Part I
PDF
java script functions, classes
PPT
Intermediate JavaScript
Javascriptinobject orientedway-090512225827-phpapp02
Object oriented java script
Awesomeness of JavaScript…almost
JavaScript Basics and Best Practices - CC FE & UX
Advanced JavaScript
Javascript Best Practices and Intro to Titanium
[2015/2016] JavaScript
ECMAScript 2015
Unit-3.pptx node js ppt documents semester-5
Java script Techniques Part I
Java scriptforjavadev part1
Javascript Performance
Javascript closures
 
All of javascript
Solid JavaScript Coding
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
Run-time of Node.js : V8 JavaScript Engine
Exciting JavaScript - Part I
java script functions, classes
Intermediate JavaScript

Recently uploaded (20)

PDF
Approach and Philosophy of On baking technology
PPTX
Big Data Technologies - Introduction.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Electronic commerce courselecture one. Pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
MYSQL Presentation for SQL database connectivity
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Machine Learning_overview_presentation.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Approach and Philosophy of On baking technology
Big Data Technologies - Introduction.pptx
Unlocking AI with Model Context Protocol (MCP)
20250228 LYD VKU AI Blended-Learning.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
A comparative analysis of optical character recognition models for extracting...
Digital-Transformation-Roadmap-for-Companies.pptx
sap open course for s4hana steps from ECC to s4
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation_ Review paper, used for researhc scholars
Programs and apps: productivity, graphics, security and other tools
Electronic commerce courselecture one. Pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Diabetes mellitus diagnosis method based random forest with bat algorithm
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
MIND Revenue Release Quarter 2 2025 Press Release
MYSQL Presentation for SQL database connectivity
The AUB Centre for AI in Media Proposal.docx
Machine Learning_overview_presentation.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx

Effective JavaScript

Editor's Notes

  • #12: No block scope. In general you reference enclosing functions and their arguments until you reach the global scope. With is an exception.
  • #13: This isn’t as true in v8.
  • #14: Referencing a is 4 steps away!
  • #15: If something isn’t defined in the object in your with statement, you create a global. If it is, you overwrite that property. It’s difficult to tell from reading the code how an assignment might affect the program.
  • #19: And you can combine the two!
  • #20: This avoids a step on the scope chain.
  • #21: Minimize your code, don’t keep functions around that are one-offs. On the other hand, this function is recreated every time.
  • #22: As always with closures, watch for memory leaks.
  • #31: Be careful though, unlike other languages with getters and setters, Javascript won’t warn you if you access an undefined property. It’ll just return undefined.