Skip to content

Instantly share code, notes, and snippets.

@olleharstedt
Last active December 30, 2020 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save olleharstedt/eaaf1dd40be541f84aa0f3954a0ea54a to your computer and use it in GitHub Desktop.
Save olleharstedt/eaaf1dd40be541f84aa0f3954a0ea54a to your computer and use it in GitHub Desktop.
Clone benchmark
class Query {
constructor() {
this.select = '';
this.from = '';
this.where = '';
}
}
for (let i = 0; i < 1000000; i++) {
const q = new Query();
const q1 = {...q, select: '1'};
const q2 = {...q1, from: 'table'};
const q3 = {...q2, where: 'a = b'};
}
/*
for (let i = 0; i < 1000000; i++) {
const q = new Query();
q.select = '1';
q.from = 'table';
q.where = 'a = b';
}
*/
type query = {
select: string;
from: string;
where: string;
}
type query_mut = {
mutable select: string;
mutable from: string;
mutable where: string;
}
let _ =
for i = 0 to 1000000 do
let q : query = {select = ""; from = ""; where = ""} in
let q1 = {q with select = "1"} in
let q2 = {q1 with from = "table"} in
let q3 = {q2 with where = "a = b"} in
()
done;
(*
for i = 0 to 1000000 do
let q : query_mut = {select = ""; from = ""; where = ""} in
q.select <- "1";
q.from <- "table";
q.where <- "a = b";
()
done
*)
<?php
class QueryBuilder
{
public $select;
public $from;
public $where;
public function withSelect($sel)
{
$that = clone $this;
$that->select = $sel;
return $that;
}
public function withFrom($from)
{
$that = clone $this;
$that->from = $from;
return $that;
}
public function withWhere($where)
{
$that = clone $this;
$that->where = $where;
return $that;
}
}
for ($i = 0; $i < 1000000; $i++) {
$q = (new QueryBuilder())->withSelect('1')->withFrom('table')->withWhere('a = b');
unset($q);
}
/*
for ($i = 0; $i < 1000000; $i++) {
$q = new QueryBuilder();
$q->select = '1';
$q->from = 'table';
$q->where = 'a = b';
unset($q);
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment