SlideShare ist ein Scribd-Unternehmen logo
1 von 66
Downloaden Sie, um offline zu lesen
DATA STRUCTURES
IN JAVASCRIPT 2015
Nir Kaufman
Nir Kaufman
- AngularJS evangelist
- International speaker
- Guitar player
Head of Angular Development @ 500Tech
*Photoshop
INTRO
DATA STRUCTURES
ARE IMPORTANT
https://www.amazon.com/Algorithms-Structures-Prentice-Hall-
Automatic-Computation/dp/0130224189
JAVASCRIPT GOT
JUST ARRAYS…
WE NEED MORE.
MAPS
SETS
LISTS
STACKS
QUEUES
TREES
2015
WE GOT A GREEN LIGHT!
kangax.github.io/compat-table/es6
AGENDA
Array improvements
Introduction to Sets
Introduction to Maps
Next steps
CODE? OR BORED?
ENTER ARRAYS
Array.of()
creates a new Array instance
with a variable number of
arguments
Array.of(50);

=> [50]
Array(50);

=> [undefined X 50]
Array.from()
creates a new Array instance
from an array-like or iterable
object.
function sum() {

const args = Array.prototype.slice.call(arguments);

return args.reduce((total, num) => total += num );

}
function sum() {

const args = Array.from(arguments);

return args.reduce((total, num) => total += num);

}
function sum(...numbers) {

return numbers.reduce((total, num) => total += num)

}
rest parameter
const numbers = Array.of(1,2,3,4,5,6);



numbers.find( num => num > 4 );



=> 5
Array.find()
const colors = Array.of("red", "green");



colors.findIndex(color => color === "green" );



=> 1
Array.findIndex()
const numbers = [1, 2, 3, 4, 5];



numbers.fill(1);



=> [1, 1, 1, 1, 1]
Array.fill()
const numbers = [1, 2, 3, 4 ];



numbers.copyWithin(2, 0);



=> [1, 2, 1, 2]
Array.copyWithin()
USE CASE??
ENTER TYPED ARRAYS
special-purpose arrays designed to work with
numeric types. provide better performance
for arithmetic operations
a memory location that can contains
a specified number of bytes
const buffer = new ArrayBuffer(8);



buffer.byteLength;



=> 8
ArrayBuffer()
Interface for manipulating array buffers
const buffer = new ArrayBuffer(8);

const view = new DataView(buffer);



view.setFloat32(2,8);

view.getFloat32(2);



// => 8
DataView()
new Int8Array(8);

new Int16Array(8);

new Int32Array(8);

new Float32Array(8);

new Float64Array(8);

new Uint8Array(8);

new Uint8ClampedArray(8);
Type-Specific views
ARRAYS
ARE POWERFUL
DO WE NEED MORE?
TEST YOURSELF
How to create an array without duplicates?
How to test for item existence?
How to remove an item from an array?
ENTER SETS
ORDERED LIST
OF UNIQUE VALUES
const colors = new Set();



colors.add('Red');

colors.add('Green');

colors.add(‘Blue');


colors.size; // => 3
Create a Set and add element
const names = new Set();



names.add('nir');

names.add('chen');



names.has('nir');

names.delete('nir');

names.clear();
Test, delete and clear
const colors = new Set();



colors.add('Red');

colors.add('Green');



colors.forEach( (value, index) => {

console.log(`${value}, ${index}`)

});
Iterate over a Set
for (let item of set)



for (let item of set.keys())



for (let item of set.values())



for (let [key, value] of set.entries())
for of loop
let set = new Set(['Red', 'Green']);

let array = [...set];



console.log(array);



// => [ 'Red', 'Green']
Set - Array conversation
Create an
‘eliminateDuplicates’
function for arrays
function eliminateDuplicates(items){

return [...new Set(items)];

}
solution
WEAK SETS
Holds a weak reference to values.
Can only contain Objects.
Expose only: add, has and remove methods.
Values can’t be access…
USE CASE??
Create a WeakSet for for ‘tagging’ objects
instances and track their existence without
mutating them.
const map = Object.create(null);



map.position = 0;



if (map.position) {

// what are we checking?

}
property existence or zero value?
const map = Object.create(null);



map[1] = 'Nir';

map['1'] = "Ran";



console.log(map[1]);

console.log(map['1']);
two entries?
ENTER MAPS
ORDERED LIST
OF KEY-VALUE PAIRS
const roles = new Map();



const nir = {id: 1, name: "Nir"};

const ran = {id: 2, name: "ran"};



roles.set(nir, ['manager']);

roles.set(ran, ['user']);



roles.size;

// => 2
Create a Map and add element
const roles = new Map();



const nir = {id: 1, name: "Nir"};

const ran = {id: 2, name: "ran"};



roles.set(nir, ['manager']);

roles.set(ran, ['user']);



roles.has(nir);

roles.delete(nir);

roles.clear();
Test, delete and clear
const roles = new Map();



const nir = {id: 1, name: "Nir"};

const ran = {id: 2, name: "ran"};



roles.set(nir, ['manager']);

roles.set(ran, ['user']);



roles.forEach( (value, key) => {

console.log(value, key);

});
Iterate over a Map
for (let [key, value] of roles) 



for (let key of roles.keys()) 



for (let value of roles.values()) 



for (let [key, value] of roles.entries())
for of loop
WEAK MAPS
Holds a weak reference to key
The key must be an object
Expose get and set.
USE CASE??
Create a WeakMap for mapping DOM
elements to objects. when the element will
destroyed, the data will freed
EXTEND
ES6 ENABLES
SUBCLASSING
OF BUILT-IN TYPES
QUEUE DEFINED
enqueue
dequeue
peek
isEmpty
CODE? OR BORED?
class Queue extends Array {



enqueue(element) {

this.push(element)

}



dequeue() {

return this.shift();

}



peek() {

return this[0];

}



isEmpty() {

return this.length === 0;

}



}
NEXT STEPS
egghead.io/courses/javascript-arrays-in-depth
THANK YOU!
nir@500tech.com
@nirkaufman
il.linkedin.com/in/nirkaufman
github.com/nirkaufman
ANGULARJS IL
meetup.com/Angular-AfterHours/
meetup.com/AngularJS-IL
Nir Kaufman

Weitere ähnliche Inhalte

Was ist angesagt?

Constants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaConstants, Variables and Data Types in Java
Constants, Variables and Data Types in Java
Abhilash Nair
 

Was ist angesagt? (20)

Basics of reflection in java
Basics of reflection in javaBasics of reflection in java
Basics of reflection in java
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
Loops in c
Loops in cLoops in c
Loops in c
 
java-thread
java-threadjava-thread
java-thread
 
Angular Advanced Routing
Angular Advanced RoutingAngular Advanced Routing
Angular Advanced Routing
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithm
 
L9 wrapper classes
L9 wrapper classesL9 wrapper classes
L9 wrapper classes
 
Intent, Service and BroadcastReciver (2).ppt
Intent, Service and BroadcastReciver (2).pptIntent, Service and BroadcastReciver (2).ppt
Intent, Service and BroadcastReciver (2).ppt
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 
Chapter 2 Flutter Basics Lecture 1.pptx
Chapter 2 Flutter Basics Lecture 1.pptxChapter 2 Flutter Basics Lecture 1.pptx
Chapter 2 Flutter Basics Lecture 1.pptx
 
C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template Library
 
Graphs
GraphsGraphs
Graphs
 
Python ppt
Python pptPython ppt
Python ppt
 
Dictionary in python
Dictionary in pythonDictionary in python
Dictionary in python
 
Namespaces in C#
Namespaces in C#Namespaces in C#
Namespaces in C#
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
 
Constants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaConstants, Variables and Data Types in Java
Constants, Variables and Data Types in Java
 

Andere mochten auch

Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
Varun C M
 

Andere mochten auch (20)

Up & running with ECMAScript6
Up & running with ECMAScript6Up & running with ECMAScript6
Up & running with ECMAScript6
 
redux and angular - up and running
redux and angular - up and runningredux and angular - up and running
redux and angular - up and running
 
Solid angular
Solid angularSolid angular
Solid angular
 
Angular Pipes Workshop
Angular Pipes WorkshopAngular Pipes Workshop
Angular Pipes Workshop
 
Redux with angular 2 - workshop 2016
Redux with angular 2 - workshop 2016Redux with angular 2 - workshop 2016
Redux with angular 2 - workshop 2016
 
How Angular2 Can Improve Your AngularJS Apps Today!
How Angular2 Can Improve Your AngularJS Apps Today!How Angular2 Can Improve Your AngularJS Apps Today!
How Angular2 Can Improve Your AngularJS Apps Today!
 
Angularjs - Unit testing introduction
Angularjs - Unit testing introductionAngularjs - Unit testing introduction
Angularjs - Unit testing introduction
 
Angular2 workshop
Angular2 workshopAngular2 workshop
Angular2 workshop
 
Webstorm
WebstormWebstorm
Webstorm
 
Angularjs - lazy loading techniques
Angularjs - lazy loading techniques Angularjs - lazy loading techniques
Angularjs - lazy loading techniques
 
JavaScript Variables
JavaScript VariablesJavaScript Variables
JavaScript Variables
 
JavaScript Tools and Implementation
JavaScript Tools and ImplementationJavaScript Tools and Implementation
JavaScript Tools and Implementation
 
Js objects
Js objectsJs objects
Js objects
 
JavaScript: Values, Types and Variables
JavaScript: Values, Types and VariablesJavaScript: Values, Types and Variables
JavaScript: Values, Types and Variables
 
Webpack and angularjs
Webpack and angularjsWebpack and angularjs
Webpack and angularjs
 
AngularJS - Services
AngularJS - ServicesAngularJS - Services
AngularJS - Services
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
Angular js - 10 reasons to choose angularjs
Angular js - 10 reasons to choose angularjs Angular js - 10 reasons to choose angularjs
Angular js - 10 reasons to choose angularjs
 
Angular redux
Angular reduxAngular redux
Angular redux
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing options
 

Ähnlich wie Data Structures in javaScript 2015

package lab7 public class SetOperations public static.pdf
package lab7     public class SetOperations  public static.pdfpackage lab7     public class SetOperations  public static.pdf
package lab7 public class SetOperations public static.pdf
syedabdul78662
 
Retro gaming with lambdas stephen chin
Retro gaming with lambdas   stephen chinRetro gaming with lambdas   stephen chin
Retro gaming with lambdas stephen chin
NLJUG
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
Rahul04August
 
An introduction to property-based testing
An introduction to property-based testingAn introduction to property-based testing
An introduction to property-based testing
Vincent Pradeilles
 

Ähnlich wie Data Structures in javaScript 2015 (20)

Collection and framework
Collection and frameworkCollection and framework
Collection and framework
 
Data Types and Processing in ES6
Data Types and Processing in ES6Data Types and Processing in ES6
Data Types and Processing in ES6
 
Collections In Scala
Collections In ScalaCollections In Scala
Collections In Scala
 
Coding in Style
Coding in StyleCoding in Style
Coding in Style
 
A bit about Scala
A bit about ScalaA bit about Scala
A bit about Scala
 
Collections Framework
Collections FrameworkCollections Framework
Collections Framework
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
package lab7 public class SetOperations public static.pdf
package lab7     public class SetOperations  public static.pdfpackage lab7     public class SetOperations  public static.pdf
package lab7 public class SetOperations public static.pdf
 
Arrays
ArraysArrays
Arrays
 
Rewriting Java In Scala
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In Scala
 
java set1 program.pdf
java set1 program.pdfjava set1 program.pdf
java set1 program.pdf
 
SDC - Einführung in Scala
SDC - Einführung in ScalaSDC - Einführung in Scala
SDC - Einführung in Scala
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
Internal workshop es6_2015
Internal workshop es6_2015Internal workshop es6_2015
Internal workshop es6_2015
 
Retro gaming with lambdas stephen chin
Retro gaming with lambdas   stephen chinRetro gaming with lambdas   stephen chin
Retro gaming with lambdas stephen chin
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
 
An introduction to property-based testing
An introduction to property-based testingAn introduction to property-based testing
An introduction to property-based testing
 
Javascript Basics
Javascript BasicsJavascript Basics
Javascript Basics
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 

Mehr von Nir Kaufman

Mehr von Nir Kaufman (13)

Angular Dependency Injection
Angular Dependency InjectionAngular Dependency Injection
Angular Dependency Injection
 
Angular Prestige: Less-known API and techniques
Angular Prestige: Less-known API and techniquesAngular Prestige: Less-known API and techniques
Angular Prestige: Less-known API and techniques
 
Angular CLI custom builders
Angular CLI custom buildersAngular CLI custom builders
Angular CLI custom builders
 
Electronic music 101 for developers
Electronic music 101 for developersElectronic music 101 for developers
Electronic music 101 for developers
 
Nestjs MasterClass Slides
Nestjs MasterClass SlidesNestjs MasterClass Slides
Nestjs MasterClass Slides
 
Redux pattens - JSHeroes 2018
Redux pattens - JSHeroes 2018Redux pattens - JSHeroes 2018
Redux pattens - JSHeroes 2018
 
Angular EE - Special Workshop by Nir Kaufman
Angular EE - Special Workshop by Nir KaufmanAngular EE - Special Workshop by Nir Kaufman
Angular EE - Special Workshop by Nir Kaufman
 
Boosting Angular runtime performance
Boosting Angular runtime performanceBoosting Angular runtime performance
Boosting Angular runtime performance
 
Decorators in js
Decorators in jsDecorators in js
Decorators in js
 
Styling recipes for Angular components
Styling recipes for Angular componentsStyling recipes for Angular components
Styling recipes for Angular components
 
Introduction To Angular's reactive forms
Introduction To Angular's reactive formsIntroduction To Angular's reactive forms
Introduction To Angular's reactive forms
 
Angular2 - getting-ready
Angular2 - getting-ready Angular2 - getting-ready
Angular2 - getting-ready
 
AngularJS performance & production tips
AngularJS performance & production tipsAngularJS performance & production tips
AngularJS performance & production tips
 

Kürzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 

Data Structures in javaScript 2015