made with esnextbin
Last active
October 28, 2016 10:40
-
-
Save trxcllnt/dbf33be19d0f1ca96c220c4cf5362a2e to your computer and use it in GitHub Desktop.
esnextbin sketch
This file contains hidden or 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 charset="utf-8"> | |
<title>ESNextbin Sketch</title> | |
<!-- put additional styles and scripts here --> | |
</head> | |
<body> | |
<!-- put markup and other contents here --> | |
</body> | |
</html> |
This file contains hidden or 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 $now = 0; | |
var $never = 1; | |
var $ref = 'ref'; | |
var now = Date.now; | |
// var arr = new Array(3); | |
console.clear(); | |
const cache = getCache(); | |
const reference = cache.overReference; | |
// debugger; | |
console.log(optimizeRef(cache, reference)); | |
// console.log(optimizeRef(cache, reference)); | |
console.log(cache); | |
function optimizeRef(cache, ref, optimized, followedRefsCount) { | |
var arr = new Array(3); | |
if (followedRefsCount === 50) { | |
throw new Error( | |
"Followed " + followedRefsCount + " references. " + | |
"This might indicate the presence of an indirect " + | |
"circular reference chain." | |
); | |
} | |
optimized = optimized || []; | |
followedRefsCount = followedRefsCount || 0; | |
var key, type, path, | |
node, next, start = ref, | |
success, depth, maxDepth, | |
optimizedLength = 0; | |
outer: do { | |
depth = 0; | |
next = cache; | |
success = false; | |
type = undefined; | |
node = undefined; | |
path = ref.value; | |
optimizedLength = 0; | |
maxDepth = path.length; | |
if (node = ref.context) { | |
next = node; | |
if (next.$type === $ref) { | |
ref = next; | |
continue; | |
} | |
while (depth < maxDepth) { | |
optimized[optimizedLength++] = path[depth++]; | |
} | |
} else { | |
while (next && !type && depth < maxDepth) { | |
node = next; | |
if (next = node[key = path[depth++]]) { | |
optimized[optimizedLength++] = key; | |
} | |
while (next && (type = next.$type) === $ref && depth < maxDepth) { | |
[next, optimized, ref] = optimizeRef( | |
cache, next, optimized, followedRefsCount + 1 | |
); | |
optimizedLength = optimized.length; | |
} | |
} | |
if (optimizedLength !== optimized.length) { | |
optimized.length = optimizedLength; | |
} | |
if (type !== undefined && isExpired(next)) { | |
break; | |
} else if (depth === maxDepth) { | |
if (type === $ref) { | |
ref = next; | |
continue; | |
} | |
ref.context = next; | |
} | |
} | |
break; | |
} while (true); | |
if (start.$optimized === false && depth === maxDepth) { | |
start.$optimized = true; | |
start.context = next; | |
start.value = optimized.slice(0, optimizedLength); | |
} | |
arr[0] = next; | |
arr[1] = optimized; | |
arr[2] = ref; | |
return arr; | |
} | |
function ref (path, optimized = true) { | |
return { $type: $ref, $optimized: optimized, value: path }; | |
} | |
function atom(value) { | |
return { $type: 'atom', value: value }; | |
} | |
function isExpired(node) { | |
var exp = node.$expires; | |
return (exp != null) && ( | |
exp !== $never ) && ( | |
exp === $now || exp < now()); | |
}; | |
function getCache() { | |
return { | |
toReference: ref(['to', 'reference']), | |
overReference: ref(['toReference'], false), | |
short: ref(['toShort', 'next']), | |
circular: ref(['circular', 'next']), | |
referencesSelf: ref(['referencesSelf']), | |
expired: { | |
title: atom('Expired Title', { $expires: 0 }) | |
}, | |
toExpiredTitle: ref(['expired', 'title']), | |
to: { | |
reference: ref(['too']), | |
toValue: ref(['too', 'title']), | |
title: 'Title' | |
}, | |
too: { | |
title: 'Title' | |
}, | |
toShort: 'Short' | |
}; | |
} |
This file contains hidden or 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
{ | |
"name": "esnextbin-sketch", | |
"version": "0.0.0", | |
"dependencies": { | |
"babel-runtime": "6.11.6" | |
} | |
} |
This file contains hidden or 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
"use strict"; | |
var _slicedToArray2 = require("babel-runtime/helpers/slicedToArray"); | |
var _slicedToArray3 = _interopRequireDefault(_slicedToArray2); | |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
var $now = 0; | |
var $never = 1; | |
var $ref = 'ref'; | |
var now = Date.now; | |
// var arr = new Array(3); | |
console.clear(); | |
var cache = getCache(); | |
var reference = cache.overReference; | |
// debugger; | |
console.log(optimizeRef(cache, reference)); | |
// console.log(optimizeRef(cache, reference)); | |
console.log(cache); | |
function optimizeRef(cache, ref, optimized, followedRefsCount) { | |
var arr = new Array(3); | |
if (followedRefsCount === 50) { | |
throw new Error("Followed " + followedRefsCount + " references. " + "This might indicate the presence of an indirect " + "circular reference chain."); | |
} | |
optimized = optimized || []; | |
followedRefsCount = followedRefsCount || 0; | |
var key, | |
type, | |
path, | |
node, | |
next, | |
start = ref, | |
success, | |
depth, | |
maxDepth, | |
optimizedLength = 0; | |
outer: do { | |
depth = 0; | |
next = cache; | |
success = false; | |
type = undefined; | |
node = undefined; | |
path = ref.value; | |
optimizedLength = 0; | |
maxDepth = path.length; | |
if (node = ref.context) { | |
next = node; | |
if (next.$type === $ref) { | |
ref = next; | |
continue; | |
} | |
while (depth < maxDepth) { | |
optimized[optimizedLength++] = path[depth++]; | |
} | |
} else { | |
while (next && !type && depth < maxDepth) { | |
node = next; | |
if (next = node[key = path[depth++]]) { | |
optimized[optimizedLength++] = key; | |
} | |
while (next && (type = next.$type) === $ref && depth < maxDepth) { | |
var _optimizeRef = optimizeRef(cache, next, optimized, followedRefsCount + 1); | |
var _optimizeRef2 = (0, _slicedToArray3.default)(_optimizeRef, 3); | |
next = _optimizeRef2[0]; | |
optimized = _optimizeRef2[1]; | |
ref = _optimizeRef2[2]; | |
optimizedLength = optimized.length; | |
} | |
} | |
if (optimizedLength !== optimized.length) { | |
optimized.length = optimizedLength; | |
} | |
if (type !== undefined && isExpired(next)) { | |
break; | |
} else if (depth === maxDepth) { | |
if (type === $ref) { | |
ref = next; | |
continue; | |
} | |
ref.context = next; | |
} | |
} | |
break; | |
} while (true); | |
if (start.$optimized === false && depth === maxDepth) { | |
start.$optimized = true; | |
start.context = next; | |
start.value = optimized.slice(0, optimizedLength); | |
} | |
arr[0] = next; | |
arr[1] = optimized; | |
arr[2] = ref; | |
return arr; | |
} | |
function ref(path) { | |
var optimized = arguments.length <= 1 || arguments[1] === undefined ? true : arguments[1]; | |
return { $type: $ref, $optimized: optimized, value: path }; | |
} | |
function atom(value) { | |
return { $type: 'atom', value: value }; | |
} | |
function isExpired(node) { | |
var exp = node.$expires; | |
return exp != null && exp !== $never && (exp === $now || exp < now()); | |
}; | |
function getCache() { | |
return { | |
toReference: ref(['to', 'reference']), | |
overReference: ref(['toReference'], false), | |
short: ref(['toShort', 'next']), | |
circular: ref(['circular', 'next']), | |
referencesSelf: ref(['referencesSelf']), | |
expired: { | |
title: atom('Expired Title', { $expires: 0 }) | |
}, | |
toExpiredTitle: ref(['expired', 'title']), | |
to: { | |
reference: ref(['too']), | |
toValue: ref(['too', 'title']), | |
title: 'Title' | |
}, | |
too: { | |
title: 'Title' | |
}, | |
toShort: 'Short' | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment