##// END OF EJS Templates
Speed up getting new post count for favorites. Get favorites at page start, not only by JS
Speed up getting new post count for favorites. Get favorites at page start, not only by JS

File last commit:

r1431:ead4e49c default
r1455:9b275148 default
Show More
proof_of_work.js
54 lines | 1.5 KiB | application/javascript | JavascriptLexer
var POW_COMPUTING_TIMEOUT = 2;
var POW_HASH_LENGTH = 16;
function computeHash(iteration, guess, target, payload, timestamp, hasher) {
iteration += 1;
var hash = hasher(payload + iteration).toString();
guess = hash.substring(0, POW_HASH_LENGTH);
if (guess <= target) {
//console.log("Iteration: ", iteration);
//console.log("Guess: ", guess);
//console.log("Target: ", target);
var data = {
iteration: iteration,
timestamp: timestamp,
guess: guess
};
self.postMessage(data);
} else {
//console.log("Iteration: ", iteration);
//console.log("Guess: ", guess);
//console.log("Target: ", target);
setTimeout(function() {
computeHash(iteration, guess, target, payload, timestamp, hasher);
}, POW_COMPUTING_TIMEOUT);
}
}
function doWork(message, hasher, difficulty) {
var timestamp = Date.now();
var iteration = 0;
var payload = timestamp + message;
var target = parseInt(Math.pow(2, POW_HASH_LENGTH * 3) / difficulty).toString();
while (target.length < POW_HASH_LENGTH) {
target = '0' + target;
}
var guess = target + '0';
setTimeout(function() {
computeHash(iteration, guess, target, payload, timestamp, hasher);
}, POW_COMPUTING_TIMEOUT);
}
self.onmessage = function(e) {
var difficulty = e.data.difficulty;
importScripts(e.data.hasher);
var hasher = CryptoJS.SHA256;
self.doWork(e.data.msg, hasher, difficulty);
};