SlideShare a Scribd company logo
1 of 7
Download to read offline
KnockoutJS 
Performance issues
if binding - causing frequent re-renders 
<!-- ko if: items().length --> 
<h2>Items</h2> 
<ul data-bind="foreach: items"> 
<li data-bind="text: name"></li> 
</ul> 
<!-- /ko --> 
<div data-bind="visible: items().length"> 
<h2>Items</h2> 
<ul data-bind="foreach: items"> 
<li data-bind="text: name"></li> 
</ul> 
</div> 
VISIBLE only hides/shows the element 
Every time IF is TRUE, the Element will re-render 
The binding will be triggered every time items().length is being changed, 
but is relevant only when length is 0 or 1 …
IF binding only when length is 0 or 1 … 
Computed observable? (no longer an issue for KO 3.0) 
this.items = ko.observableArray(); 
this.hasItems = ko.computed(function( 
{ 
return this.items().length>0 
}); 
In ko v2.x computed observables will always notify subscribers 
when re-evaluated, 
even if their value ends up being the same! 
this is not true for observables: 
[ This is solved in v3.0.0, and computed 
observables acts by default like 
observables for primitive types: 
string/boolean/number/null/undefined ] 
this.hasItems = ko.observable(false); 
this.items.subscribe(function(newValue) 
{ 
this.hasItems(newValue && newValue.length ? true : false); 
}, this);
with binding - causing frequent re-renders 
<div data-bind="with: selectedItem"> 
<h2>Selected</h2> 
<input data-bind="value: name" /> 
<input data-bind="value: price" /> 
</div> 
Whenever selectedItem changes, the contents are 
re-rendered completely: 
Instead of just binding the inputs against the new data, 
the entire section is replaced with new elements 
<div data-bind="visible: selectedItem"> 
<h2>Selected</h2> 
<input data-bind="value: selectedItem().name /> 
<input data-bind="value: selectedItem().price /> 
</div> 
<div data-bind="visible: selectedItem"> 
<h2>Selected</h2> 
<!-- ko with: selectedItem --> 
<input data-bind="value: name" /> 
<input data-bind="value: price" /> 
<!-- /ko --> 
</div> 
If we do want to use WITH, we 
should use it in smaller, targeted 
areas
Manipulating observableArrays 
var ViewModel = function() { 
this.items = ko.observableArray( 
[ new Item("Task One", "high"), new Item("Task Two", "normal"), 
new Item("Task Three", "high") ] 
); 
this.highPriorityItems = ko.computed(function() 
{ 
return ko.utils.arrayFilter(this.items(), function(item) 
{ 
return item.priority() === "high"; 
}); 
}; 
}; 
this.addNewData = function(newData) { 
var item; 
for (var i = 0, j = newData.length; i < j; i++) 
{ 
item = newData[i]; 
self.items.push(new Item(item.name, item.priority)); 
} 
}; 
On every push of new item to items, 
highPriorityItems will be recalculated! 
this.addNewData = function(newData) { 
var item, underlyingArray = self.items();  get a reference 
for (var i = 0, j = newData.length; i < j; i++) { 
item = newData[i]; 
underlyingArray.push(new Item(item.name, item.priority));  push to referance 
} 
self.items.valueHasMutated();  notify of a change once! 
};
Bindings Fire Together(no longer an issue for KO 3.0) 
<select data-bind="options: choices, 
value: selectedValue, 
visible: showChoices"> 
</select> 
In KO there is only one computed observable used to track 
all of an element’s bindings (options, value, visible) 
Meaning: change in one binding causes to all bindings update 
<div data-bind="visible: showPlaylist"> 
<select data-bind="options: choices, value: selectedValue"> 
</select> 
</div> 
Michael Best’s repeat plugin: 
<select data-bind="value: selectedOption"> 
<option data-bind="repeat: { 
foreach: items, bind: 'attr: { value: $item().id }, 
text: $item().name'}"> 
</select> 
Optimizing Select: 
<select data-bind="value: selectedValue"> 
<!– ko foreach: items() --> 
<option data-bind="value: id , text: name "> 
<!--/ko --> 
</select> 
 IE removes comments between <select> & <option>
More Reading: 
• Articles: 
• knockoutjs-performance-gotcha-1 
• knockoutjs-performance-gotcha-2 
• knockoutjs-performance-gotcha-3 (not relevant for KO 3.0+) 
• Withlight – light WITH binding for observable that won’t change 
• Reevaluating computed observables fiddle 
• Michael Best’s repeat plugin

More Related Content

Recently uploaded

What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesVictoriaMetrics
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolsosttopstonverter
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shardsChristopher Curtin
 
SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?Alexandre Beguel
 
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdfSteve Caron
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldRoberto Pérez Alcolea
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...OnePlan Solutions
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLionel Briand
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...Bert Jan Schrijver
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITmanoharjgpsolutions
 
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...kalichargn70th171
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfmaor17
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptxVinzoCenzo
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingShane Coughlan
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slidesvaideheekore1
 
Advantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxAdvantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxRTS corp
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesKrzysztofKkol1
 
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jGraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jNeo4j
 
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdfPros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdfkalichargn70th171
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorTier1 app
 

Recently uploaded (20)

What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 Updates
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration tools
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards
 
SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?
 
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository world
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and Repair
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh IT
 
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdf
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptx
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slides
 
Advantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxAdvantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptx
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
 
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jGraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
 
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdfPros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryError
 

Knockout js performance issues

  • 2. if binding - causing frequent re-renders <!-- ko if: items().length --> <h2>Items</h2> <ul data-bind="foreach: items"> <li data-bind="text: name"></li> </ul> <!-- /ko --> <div data-bind="visible: items().length"> <h2>Items</h2> <ul data-bind="foreach: items"> <li data-bind="text: name"></li> </ul> </div> VISIBLE only hides/shows the element Every time IF is TRUE, the Element will re-render The binding will be triggered every time items().length is being changed, but is relevant only when length is 0 or 1 …
  • 3. IF binding only when length is 0 or 1 … Computed observable? (no longer an issue for KO 3.0) this.items = ko.observableArray(); this.hasItems = ko.computed(function( { return this.items().length>0 }); In ko v2.x computed observables will always notify subscribers when re-evaluated, even if their value ends up being the same! this is not true for observables: [ This is solved in v3.0.0, and computed observables acts by default like observables for primitive types: string/boolean/number/null/undefined ] this.hasItems = ko.observable(false); this.items.subscribe(function(newValue) { this.hasItems(newValue && newValue.length ? true : false); }, this);
  • 4. with binding - causing frequent re-renders <div data-bind="with: selectedItem"> <h2>Selected</h2> <input data-bind="value: name" /> <input data-bind="value: price" /> </div> Whenever selectedItem changes, the contents are re-rendered completely: Instead of just binding the inputs against the new data, the entire section is replaced with new elements <div data-bind="visible: selectedItem"> <h2>Selected</h2> <input data-bind="value: selectedItem().name /> <input data-bind="value: selectedItem().price /> </div> <div data-bind="visible: selectedItem"> <h2>Selected</h2> <!-- ko with: selectedItem --> <input data-bind="value: name" /> <input data-bind="value: price" /> <!-- /ko --> </div> If we do want to use WITH, we should use it in smaller, targeted areas
  • 5. Manipulating observableArrays var ViewModel = function() { this.items = ko.observableArray( [ new Item("Task One", "high"), new Item("Task Two", "normal"), new Item("Task Three", "high") ] ); this.highPriorityItems = ko.computed(function() { return ko.utils.arrayFilter(this.items(), function(item) { return item.priority() === "high"; }); }; }; this.addNewData = function(newData) { var item; for (var i = 0, j = newData.length; i < j; i++) { item = newData[i]; self.items.push(new Item(item.name, item.priority)); } }; On every push of new item to items, highPriorityItems will be recalculated! this.addNewData = function(newData) { var item, underlyingArray = self.items();  get a reference for (var i = 0, j = newData.length; i < j; i++) { item = newData[i]; underlyingArray.push(new Item(item.name, item.priority));  push to referance } self.items.valueHasMutated();  notify of a change once! };
  • 6. Bindings Fire Together(no longer an issue for KO 3.0) <select data-bind="options: choices, value: selectedValue, visible: showChoices"> </select> In KO there is only one computed observable used to track all of an element’s bindings (options, value, visible) Meaning: change in one binding causes to all bindings update <div data-bind="visible: showPlaylist"> <select data-bind="options: choices, value: selectedValue"> </select> </div> Michael Best’s repeat plugin: <select data-bind="value: selectedOption"> <option data-bind="repeat: { foreach: items, bind: 'attr: { value: $item().id }, text: $item().name'}"> </select> Optimizing Select: <select data-bind="value: selectedValue"> <!– ko foreach: items() --> <option data-bind="value: id , text: name "> <!--/ko --> </select>  IE removes comments between <select> & <option>
  • 7. More Reading: • Articles: • knockoutjs-performance-gotcha-1 • knockoutjs-performance-gotcha-2 • knockoutjs-performance-gotcha-3 (not relevant for KO 3.0+) • Withlight – light WITH binding for observable that won’t change • Reevaluating computed observables fiddle • Michael Best’s repeat plugin