From 7b3983626a06e24920f1d3d4c49f3b4fbf37bd45 Mon Sep 17 00:00:00 2001 From: "Simon A. Eugster" Date: Wed, 14 Oct 2015 15:17:11 +0200 Subject: [PATCH] Graph structure: Object/Map/Array performance --- .../problem-graph-structure.md | 14 +- tests/es6-array-object-map.js | 121 ++++++++++++++++++ 2 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 tests/es6-array-object-map.js diff --git a/ex2-graph-structure/problem-graph-structure.md b/ex2-graph-structure/problem-graph-structure.md index b226326..feb24dd 100644 --- a/ex2-graph-structure/problem-graph-structure.md +++ b/ex2-graph-structure/problem-graph-structure.md @@ -14,12 +14,15 @@ Vertices are identified by their ID (or number), so we can use it for requesting from the graph and then, for example, get the number of neighbours. Hint: Be careful about the data structure you choose for storing vertices; see the literature section -at the end. +at the end. You can expect that JavaScript data structures like Object and Map are internally using +e.g. binary search trees. -Make sure you can answer the following questions: +Make sure you can answer the following questions. You may want to consider the literature linked below +or some literature about algorithms and binary search trees. -* Why should you not use an array? -* +* What magnitude of operations is required when searching for an element in an array vs. in a binary search tree, + for n = 10, n = 1000, and n = 100 000? (E.g., if an algorithm has complexity *O(n²)*, we would receive 100, 10⁶, 10¹⁰.) +* Why should you not use an array for the vertices? ## Task @@ -48,4 +51,5 @@ You may use the following template (obviously, you will also want to add your ed * [Big-O Cheatsheet](http://bigocheatsheet.com/) * [JavaScript Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) -* [JavaScript Arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) \ No newline at end of file +* [JavaScript Arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) +* [Array, Object, Map: Lookup](http://jsperf.com/es6-array-object-map-lookup) \ No newline at end of file diff --git a/tests/es6-array-object-map.js b/tests/es6-array-object-map.js new file mode 100644 index 0000000..86c8780 --- /dev/null +++ b/tests/es6-array-object-map.js @@ -0,0 +1,121 @@ +"use strict"; + +var S = Array.apply( null, Array( 100 ) ).map( function ( el, ix ) { + return ix; +} ); +var M = Array.apply( null, Array( 1000 ) ).map( function ( el, ix ) { + return ix; +} ); +var L = Array.apply( null, Array( 10000 ) ).map( function ( el, ix ) { + return ix; +} ); + +var rand = function ( sml ) { + return Math.floor( Math.random() * sml.length ); +}; + +var data = { + S: S, + M: M, + L: L, + smallArr: [], + smallObj: {}, + smallMap: new Map(), + medArr: [], + medObj: {}, + medMap: new Map(), + largeArr: [], + largeObj: {}, + largeMap: new Map() + }, + el, + key, + j, + N; + +S.forEach( function ( i ) { + + var obj = { + id: i, + data: 'foo' + }; + + data.smallArr.push( obj ); + data.smallObj[ i ] = obj; + data.smallMap.set( i, obj ); + +} ); + +M.forEach( function ( i ) { + + var obj = { + id: i, + data: 'foo' + }; + + data.medArr.push( obj ); + data.medObj[ i ] = obj; + data.medMap.set( i, obj ); + +} ); + +L.forEach( function ( i ) { + + var obj = { + id: i, + data: 'foo' + }; + + data.largeArr.push( obj ); + data.largeObj[ i ] = obj; + data.largeMap.set( i, obj ); + +} ); + + +key = rand( S ); +el = data.smallObj[ key ]; + +key = rand( S ); +el = data.smallMap.get( key ); + +key = rand( S ); +N = S.length; +for ( j = 0; j < N; j++ ) { + if ( data.smallArr[ j ].id === key ) { + el = data.smallArr[ j ]; + break; + } +} + + +key = rand( M ); +el = data.medObj[ key ]; + +key = rand( M ); +el = data.medMap.get( key ); + +key = rand( M ); +N = M.length; +for ( j = 0; j < N; j++ ) { + if ( data.medArr[ j ].id === key ) { + el = data.medArr[ j ]; + break; + } +} + + +key = rand( L ); +el = data.largeObj[ key ]; + +key = rand( L ); +el = data.largeMap.get( key ); + +key = rand( L ); +N = L.length; +for ( j = 0; j < N; j++ ) { + if ( data.largeArr[ j ].id === key ) { + el = data.largeArr[ j ]; + break; + } +}