Skip to content

Commit

Permalink
Graph structure: Object/Map/Array performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Granjow committed Oct 14, 2015
1 parent d48ee29 commit 7b39836
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 5 deletions.
14 changes: 9 additions & 5 deletions ex2-graph-structure/problem-graph-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
* [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)
121 changes: 121 additions & 0 deletions tests/es6-array-object-map.js
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 7b39836

Please sign in to comment.