Skip to content

Instantly share code, notes, and snippets.

kddnewton / json.rb
Last active February 26, 2023 10:58
Faster JSON parser in Ruby
View json.rb
# frozen_string_literal: true
require "bundler/inline"
gemfile do
source ""
gem "json_pure"
gem "benchmark-ips"
View globals-debugger.js
* GlobalsDebugger
* Inspect the stack when a global variable is being set on the window object.
* Given a global variable name, it proxies the variable name in the window
* object adding some custom code that will be invoked whenever the variable
* is set. The custom code will log the current stack trace and halt the code
* execution to allow inspecting the stack and context in your browser DevTools.
* You can use the "globalsToInspect" query-parameter to set a comma-separated
* list of names of the variables you want to inspect.

Cheat sheet: modules

Named exports, named imports, namespace imports

If we put export in front of a named entity inside a module, it becomes a named export of that module. All other entities are private to the module.

//===== lib1.mjs =====
// Named exports
Kukunin / ractors.rb
Last active May 14, 2023 16:19
Ruby Ractors vs Threads benchmark
View ractors.rb
require 'benchmark'
require 'etc' { :warmup } if defined?(Ractor)
def fibonacci(n)
return n if (0..1).include? n
fibonacci(n - 1) + fibonacci(n - 2)
swanson / .irbrc
Last active April 20, 2023 23:17
.irbrc color coding Rails console by environment
View .irbrc
# Add color coding based on Rails environment for safety
if defined? Rails
banner = if Rails.env.production?
"\e[41;97;1m #{Rails.env} \e[0m "
"\e[42;97;1m #{Rails.env} \e[0m "
# Build a custom prompt

for-of, early termination, iterators, and generators

If we terminate a loop early, for-of invokes method .return() of the iterator. Generators return generator objects (that implement both the Iterable interface and the Iterator interface). If we invoke .return() on such an object, the generator is terminated. We can prevent that by overriding .return().

More information:


function logIter(iter) {
View .eslintrc.json
"env": {
"browser": true,
"es6": true
"extends": [
dmshvetsov / worker_and_queue_perfect_pair.rb
Created February 5, 2019 05:33
Example of how we may use queue with threads to make efficient background worker.
View worker_and_queue_perfect_pair.rb
class Worker
def self.start(num_threads:, queue_size:)
queue =
worker = new(num_threads: num_threads, queue: queue)
def initialize(num_threads:, queue:)
@num_threads = num_threads
View inventory
hartator / benchmark-deep-vs-flat-directories.rb
Last active December 28, 2018 10:28
Source code for Benchmark: Deep directory structure vs. flat directory structure to store millions of files on ext4 article (
View benchmark-deep-vs-flat-directories.rb
require 'digest'
require 'benchmark'
hash = {}
10_000_000.times do
key = Digest::MD5.hexdigest(rand.to_s)
value = Digest::MD5.hexdigest(rand.to_s)
hash[key] = value