Skip to content

Instantly share code, notes, and snippets.

View poeschko's full-sized avatar

Jan Pöschko poeschko

View GitHub Profile
@poeschko
poeschko / index.html
Last active March 20, 2021 17:58
Array vs. Linked List #jsbench #jsperf (https://jsbench.github.io/#0b41c2ed86e39e0777a11ac5e92ba482) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Array vs. Linked List #jsbench #jsperf</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>
@poeschko
poeschko / counters-float.js
Created February 27, 2019 12:10
Performance of Smi vs. floating-point number representation
function copyAndIncrement(arr) {
const copy = arr.slice();
copy[0] += 1;
return copy;
}
function spoil() {
const objThatSpoilsEverything = {};
objThatSpoilsEverything.value = 1.5;
}
@poeschko
poeschko / countDistinct.js
Last active November 2, 2015 20:03
Count distinct items in an array
function countDistinct(items) {
var result = [];
for (var i = 0, l = items.length; i < l; ++i) {
var item = items[i];
var index = -1;
for (var j = 0; j < result.length; ++j) {
var existing = result[j];
if (item === existing[0]) {
index = j;
break;
@poeschko
poeschko / regexpsub.js
Created January 23, 2012 23:11
JavaScript regular expression substitution
function sub(pattern, string, repl) {
// Substitute all matches of pattern in string with the value returned by repl
// given a match and the corresponding group values,
// similar to Python's re.sub function.
// Note that pattern must be a "global" RegExp of the form /.../g
var found;
var lastIndex = 0;
var result = "";
while (found = pattern.exec(string)) {
var subst = repl.apply(this, found);
@poeschko
poeschko / textwidth.js
Created January 4, 2012 21:28
JavaScript text width
function cached(func) {
cache = {};
return function() {
var args = [];
for (var i = 0; i < arguments.length; ++i)
args.push(arguments[i]);
var key = $.param({args: args});
var value = cache[key];
if (typeof value != "undefined")
return value;
@poeschko
poeschko / bulk_insertion.py
Created January 3, 2012 22:14
Bulk insertion for Django models
class BulkInsertion(object):
"""
Bulk-insert several model instances using accumulated DB statements.
Usage:
>>> with BulkInsertion('app_tablename') as inserter:
... inserter.insert(field1=value1, field2=value2)
By default, a DB statement is issued every 1000 inserts.