Skip to content

Instantly share code, notes, and snippets.

@gobwas
gobwas / assert.js
Created May 29, 2014 09:11
assert.js
(function(global) { "use strict";
var each = function(obj, iterator, context) {
if (obj == null) return;
if (Array.prototype.forEach && obj.forEach === Array.prototype.forEach) {
obj.forEach(iterator, context);
} else if (isArray(obj)) {
for (var i = 0, l = obj.length; i < l; i++) {
if (iterator.call(context, obj[i], i, obj) === {}) return;
}
@gobwas
gobwas / inherits.js
Last active February 11, 2024 23:05
Inheritance function. Inspired by backbone and goog.
(function() { "use strict";
var global = this;
/**
* Each iterator.
*
* @param {object} props
* @param {function} func
* @param {object} [context]
var cowPool sync.Pool
func acquireCow(r io.Reader) *cow {
if c, ok := cowPool.Get().(*cow); ok {
c.r = r
@gobwas
gobwas / iface_test.go
Last active October 3, 2017 16:19
Alloc-free interface usage
// This example shows how interfaces could be used without
// pushing inner data to heap.
//
// Note that it was checked with Go 1.8.3 and may become unuseful
// in the future releases of Go.
package main
import (
"encoding/binary"
"testing"
@gobwas
gobwas / counter.go
Created September 29, 2017 09:53
Ref counter based on Go GC
package main
import (
"fmt"
"runtime"
"sync"
"sync/atomic"
"time"
)
@gobwas
gobwas / stack.go
Created June 18, 2017 21:02
Stack growth test
package main
import (
"encoding/binary"
"flag"
"fmt"
"log"
"os"
"reflect"
"runtime"
func log2(n uint) uint {
const uintBits = 32 << (^uint(0) >> 63)
var (
seen, bit uint
m, l, r uint
)
r = uintBits
for l < r {
m = l + (r-l)/2
@gobwas
gobwas / contains.go
Created February 1, 2017 14:09
Contains uint64 sybmol
// Say you want to search 0x22 symbol in two bytes of 0x2122
// you make mask for your search as 0x2222
// then:
//
// 00100001 00100010
// ^ 00100010 00100010
// -------------------
// 00000011 00000000 00000011 00000000
// & 01111111 11111111 (cut topmost bit) & 10000000 10000000 (save restore mask)
// ------------------- -------------------
@gobwas
gobwas / timer.go
Created January 24, 2017 15:15
Timer race condition prove.
package main
import (
"flag"
"fmt"
"sync"
"time"
)
var withLock = flag.Bool("lock", false, "use lock on resets")
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>UUID</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>