Skip to content

Instantly share code, notes, and snippets.

@wthit56
wthit56 / gist:4427971
Last active December 10, 2015 11:29 — forked from anonymous/gist:4427822
// run here (use the data-uri below as an address):
// data:text/html;ascii,<script src="https://gist.github.com/raw/4427971/cef83fed8b6de8bfbb9fa3ae461b724fff87c057/gistfile1.js" type="text/javascript"></script>
// The technique used here is based on an article by Ashley Gullen: http://netm.ag/SWI0E5
// Array#slice returns an array with the seleted range of items.
// Because a new array is created every time you use this method, however,
// all these arrays are in memory, which means the JavaScript GC (Garbage Collection)
// has to collect up all of these unused arrays and throw them away.
// In performance-critical situations, such as in a game-loop that needs to run within
@wthit56
wthit56 / gist:4427983
Last active December 10, 2015 11:29
Reusing objects to avoid Garbage Collection
// run here (use the data-uri below as an address):
// data:text/html;ascii,<script src="https://gist.github.com/raw/4427983/9bd86a469ad84effee2daffad03909a25d4200a2/gistfile1.js" type="text/javascript"></script>
var Reusable = (function () {
// used to cache any clean objects for reuse
var clean = [];
function Reusable(value) {
console.group("creating new object");
var _ = this;
if (!window.BakeCookies) {
window.BakeCookies = {
set: (function () {
var days;
return function BakeCookies_set(name, value, expires) {
if ((expires != null) && !(expires instanceof Date)) {
switch (typeof (expires)) {
case "number":
days = expires;

Async Techniques

NOTE: this project is for ideas regarding how to work with asynchronous calls. It includes ways one might achieve different effects and flows, in the form of simple, commented code. It does not include libraries or reusable code of any kind; this is to help programmers understand when to use certain techniques and how such techniques can be implemented.

When working with an asynchronous framework such as Node.js, you will often use code like the following:

@wthit56
wthit56 / reusable Obj class
Created June 29, 2013 12:09
Garbage Collection is a very useful and clever piece of the JavaScript engine. It cleans up pieces of memory that are hanging around for no reason, allowing the machine it is running on to continue running as smoothly as possible without having to move large chunks of memory to and from the hard drive to try to cope. There are times, however, in…
// holds all instantiated Obj objects in memory for reuse
var clean = [];
function Obj(a, b) {
// reset newObj variable
var newObj = false;
// when function was not called as a "new" contructor...
// (like a "factory" function)
if (!(this instanceof Obj)) {
@wthit56
wthit56 / CreateCallback.js
Last active August 29, 2015 14:04
CreateCallback.js, a callback creator/manager for client or server-side
var CreateCallback = (function() {
var pool = [];
function CreateCallback() {
if(pool.length){
var callback = pool.pop();
}
else {
function callback() {
if(callback.action){
@wthit56
wthit56 / .md
Last active August 29, 2015 14:17
Writing Your Own Non-Templating Engine

Writing Your Own Non-Templating Engine

Templating engines are a dime-a-dozen right now, ranging from micro-DSLs to opinionated behemoth frameworks. It can be a bit of a chore to wade through them all (even with a Template Chooser), and even when you settle on one to use, it may not have all the functionality you want. So what do you do? You write your own, with as many custom and customizable features as you wish.

But writing a templating engine can be a whole mess of a problem all by itself. Writing a custom DSL that won't break with a mis-placed space or capital letter, and does what you want it to do, and ready for future changes, improvements, and additions, is just a pain the behind.

So what would the ideal-world, blue-sky apex of templating engines look like? What would you be able to do with it? If we're talking about JavaScript and HTML (and in this article, for example's sake, I will be), you'd want to be able to use all the power and simplicity

@wthit56
wthit56 / .md
Last active August 29, 2015 14:17
Using Your Own Non-Templating Engine

Please read Writing Your Own Non-Templating Engine before reading this article.

Using Your Own Non-Templating Engine

In the previous article (link at the top), I showed how you can write a very simple function that will turn a given template string into something JavaScript can use. In this article, I'll go into how you can use this in practise.

The Basics

First, let's just make sure we're on the same page as to what the code is and what it does. And while we're at it, let's wrap it up in a function we can reference in later code examples:

"string" + 1 == "string1"
var data = { heading: "Page Title" };
data + "" === "[object Object]"