Created
December 31, 2015 02:16
-
-
Save anonymous/f59e6b709105a3401d80 to your computer and use it in GitHub Desktop.
JS Bin RxJS Throttle vs Sample // source http://jsbin.com/jepeji
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="RxJS Throttle vs Sample"> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/2.5.3/rx.all.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/2.5.3/rx.time.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/2.5.3/rx.virtualtime.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/2.5.3/rx.testing.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<style> | |
td, th { | |
border: thin black solid | |
} | |
</style> | |
<body> | |
<table> | |
<thead> | |
<tr> | |
<th>Function</th> | |
<th>Value</th> | |
<th>Last Update Delay(ms)</th> | |
<th>Update Count</th> | |
</tr> | |
</thead> | |
<tbody> | |
<tr id='debounce'></tr> | |
<tr id='throttle'></tr> | |
<tr id='throttleFirst'></tr> | |
<tr id='sample'></tr> | |
</tbody> | |
</table> | |
<script id="jsbin-javascript"> | |
var moves = Rx.Observable.fromEvent(document, 'mousemove'); | |
var DELAY_MS = 300; | |
var dbCount = 0, | |
thrCount = 0, | |
thrFCount = 0, | |
samCount = 0; | |
var dataMap = {}; | |
function setText(id, event, dataItem) { | |
var tdOpen = '<td>', | |
tdClose = '</td>'; | |
var rowHtml = makeColumn(id) + | |
makeColumn(event.x + ', ' + event.y) + | |
makeColumn(dataItem.delayBtwnUpdates * 0.001) + | |
makeColumn(dataItem.count); | |
document.getElementById(id).innerHTML = rowHtml; | |
function makeColumn(text) { | |
return tdOpen + text + tdClose; | |
} | |
} | |
function updateItem(id) { | |
if (!dataMap[id]) { | |
dataMap[id] = { | |
'count': 1, | |
'delayBtwnUpdates': 0 | |
}; | |
} else { | |
dataMap[id].count = ++dataMap[id].count; | |
dataMap[id].delayBtwnUpdates = (Date.now() - dataMap[id].delayBtwnUpdates); | |
} | |
return dataMap[id]; | |
} | |
moves.debounce(DELAY_MS).subscribe(function(event) { | |
var id = 'debounce'; | |
setText(id, event, updateItem(id)); | |
}); | |
moves.throttle(DELAY_MS).subscribe(function(event) { | |
var id = 'throttle'; | |
setText(id, event, updateItem(id)); | |
}); | |
moves.throttleFirst(DELAY_MS).subscribe(function(event) { | |
var id = 'throttleFirst'; | |
setText(id, event, updateItem(id)); | |
}); | |
moves.sample(DELAY_MS).subscribe(function(event) { | |
var id = 'sample'; | |
setText(id, event, updateItem(id)); | |
}); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var moves = Rx.Observable.fromEvent(document, 'mousemove'); | |
var DELAY_MS = 300; | |
var dbCount = 0, | |
thrCount = 0, | |
thrFCount = 0, | |
samCount = 0; | |
var dataMap = {}; | |
function setText(id, event, dataItem) { | |
var tdOpen = '<td>', | |
tdClose = '</td>'; | |
var rowHtml = makeColumn(id) + | |
makeColumn(event.x + ', ' + event.y) + | |
makeColumn(dataItem.delayBtwnUpdates * 0.001) + | |
makeColumn(dataItem.count); | |
document.getElementById(id).innerHTML = rowHtml; | |
function makeColumn(text) { | |
return tdOpen + text + tdClose; | |
} | |
} | |
function updateItem(id) { | |
if (!dataMap[id]) { | |
dataMap[id] = { | |
'count': 1, | |
'delayBtwnUpdates': 0 | |
}; | |
} else { | |
dataMap[id].count = ++dataMap[id].count; | |
dataMap[id].delayBtwnUpdates = (Date.now() - dataMap[id].delayBtwnUpdates); | |
} | |
return dataMap[id]; | |
} | |
moves.debounce(DELAY_MS).subscribe(function(event) { | |
var id = 'debounce'; | |
setText(id, event, updateItem(id)); | |
}); | |
moves.throttle(DELAY_MS).subscribe(function(event) { | |
var id = 'throttle'; | |
setText(id, event, updateItem(id)); | |
}); | |
moves.throttleFirst(DELAY_MS).subscribe(function(event) { | |
var id = 'throttleFirst'; | |
setText(id, event, updateItem(id)); | |
}); | |
moves.sample(DELAY_MS).subscribe(function(event) { | |
var id = 'sample'; | |
setText(id, event, updateItem(id)); | |
});</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var moves = Rx.Observable.fromEvent(document, 'mousemove'); | |
var DELAY_MS = 300; | |
var dbCount = 0, | |
thrCount = 0, | |
thrFCount = 0, | |
samCount = 0; | |
var dataMap = {}; | |
function setText(id, event, dataItem) { | |
var tdOpen = '<td>', | |
tdClose = '</td>'; | |
var rowHtml = makeColumn(id) + | |
makeColumn(event.x + ', ' + event.y) + | |
makeColumn(dataItem.delayBtwnUpdates * 0.001) + | |
makeColumn(dataItem.count); | |
document.getElementById(id).innerHTML = rowHtml; | |
function makeColumn(text) { | |
return tdOpen + text + tdClose; | |
} | |
} | |
function updateItem(id) { | |
if (!dataMap[id]) { | |
dataMap[id] = { | |
'count': 1, | |
'delayBtwnUpdates': 0 | |
}; | |
} else { | |
dataMap[id].count = ++dataMap[id].count; | |
dataMap[id].delayBtwnUpdates = (Date.now() - dataMap[id].delayBtwnUpdates); | |
} | |
return dataMap[id]; | |
} | |
moves.debounce(DELAY_MS).subscribe(function(event) { | |
var id = 'debounce'; | |
setText(id, event, updateItem(id)); | |
}); | |
moves.throttle(DELAY_MS).subscribe(function(event) { | |
var id = 'throttle'; | |
setText(id, event, updateItem(id)); | |
}); | |
moves.throttleFirst(DELAY_MS).subscribe(function(event) { | |
var id = 'throttleFirst'; | |
setText(id, event, updateItem(id)); | |
}); | |
moves.sample(DELAY_MS).subscribe(function(event) { | |
var id = 'sample'; | |
setText(id, event, updateItem(id)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment