Skip to content

Instantly share code, notes, and snippets.

View alexbezhan's full-sized avatar
🎯
Focusing

Aleksandr Bezhan alexbezhan

🎯
Focusing
View GitHub Profile
@alexbezhan
alexbezhan / swap.js
Created March 2, 2023 18:52
How to unwrap TRs if they got wrapped in a Tbody by the DOMParser
// unwrap TRs if they got wrapped in a tbody by the parser
{
let trsCountInResponseText = 0;
let trsCountInResponseFragment = 0;
if (responseText.indexOf('<tr') !== -1) {
var regExp = new RegExp('<tr', 'gim');
trsCountInResponseText = (responseText.match(regExp) || []).length;
for (const elt of responseElts) {
if (elt.tagName === 'TR') {
@alexbezhan
alexbezhan / laser.js
Created February 26, 2023 23:47
Laser.js - fast and small HTMX-like library
/**
* Principles:
* 1. Performance
* 2. Simplicity and debuggability.
*
* It's not trying to be:
* 1. Flexible.
*
* This results into the following:
* 1. Explicit attributes only
```js
htmx.onLoad(function (content) {
const attr = 'lr-post';
const httpMethod = 'POST';
const controlElts = content.querySelectorAll(`[${attr}]`);
for (const controlElt of controlElts) {
const link = controlElt.getAttribute && controlElt.getAttribute(attr);
if (link === undefined) {
continue
}
@alexbezhan
alexbezhan / Profile-20230221T183030.json
Created February 21, 2023 18:31
Commented out beforeCleanupElement
This file has been truncated, but you can view the full file.
[{"args":{"name":"swapper"},"cat":"__metadata","name":"thread_name","ph":"M","pid":0,"tid":0,"ts":0},
{"args":{"name":"Compositor"},"cat":"__metadata","name":"thread_name","ph":"M","pid":87078,"tid":42755,"ts":0},
{"args":{"name":"CrBrowserMain"},"cat":"__metadata","name":"thread_name","ph":"M","pid":87047,"tid":259,"ts":0},
{"args":{"name":"CrRendererMain"},"cat":"__metadata","name":"thread_name","ph":"M","pid":87078,"tid":259,"ts":0},
{"args":{"name":"CrGpuMain"},"cat":"__metadata","name":"thread_name","ph":"M","pid":87058,"tid":259,"ts":0},
{"args":{"name":"Chrome_IOThread"},"cat":"__metadata","name":"thread_name","ph":"M","pid":87047,"tid":30979,"ts":0},
{"args":{"name":"ThreadPoolBackgroundWorker"},"cat":"__metadata","name":"thread_name","ph":"M","pid":87058,"tid":25091,"ts":0},
{"args":{"name":"VizCompositorThread"},"cat":"__metadata","name":"thread_name","ph":"M","pid":87058,"tid":37379,"ts":0},
{"args":{"name":"Chrome_ChildIOThread"},"cat":"__metadata","name":"thread_name","ph":"M","pid":87078,"tid":1
@alexbezhan
alexbezhan / swap-oob-slow-profile.json
Created February 21, 2023 18:00
HTMX profile - swap oob slow
This file has been truncated, but you can view the full file.
[{"args":{"name":"swapper"},"cat":"__metadata","name":"thread_name","ph":"M","pid":0,"tid":0,"ts":0},
{"args":{"name":"CrBrowserMain"},"cat":"__metadata","name":"thread_name","ph":"M","pid":87047,"tid":259,"ts":0},
{"args":{"name":"Compositor"},"cat":"__metadata","name":"thread_name","ph":"M","pid":87078,"tid":42755,"ts":0},
{"args":{"name":"CrRendererMain"},"cat":"__metadata","name":"thread_name","ph":"M","pid":87078,"tid":259,"ts":0},
{"args":{"name":"Chrome_IOThread"},"cat":"__metadata","name":"thread_name","ph":"M","pid":87047,"tid":30979,"ts":0},
{"args":{"name":"CrGpuMain"},"cat":"__metadata","name":"thread_name","ph":"M","pid":87058,"tid":259,"ts":0},
{"args":{"name":"ThreadPoolBackgroundWorker"},"cat":"__metadata","name":"thread_name","ph":"M","pid":87058,"tid":25091,"ts":0},
{"args":{"name":"VizCompositorThread"},"cat":"__metadata","name":"thread_name","ph":"M","pid":87058,"tid":37379,"ts":0},
{"args":{"name":"Chrome_ChildIOThread"},"cat":"__metadata","name":"thread_name","ph":"M","pid":87078,"tid":1
@alexbezhan
alexbezhan / slow-swap.json
Created February 10, 2023 11:24
HTMX slow swap
This file has been truncated, but you can view the full file.
[{"args":{"name":"swapper"},"cat":"__metadata","name":"thread_name","ph":"M","pid":0,"tid":0,"ts":0},
{"args":{"name":"Compositor"},"cat":"__metadata","name":"thread_name","ph":"M","pid":26442,"tid":22019,"ts":0},
{"args":{"name":"CrBrowserMain"},"cat":"__metadata","name":"thread_name","ph":"M","pid":26404,"tid":259,"ts":0},
{"args":{"name":"CrRendererMain"},"cat":"__metadata","name":"thread_name","ph":"M","pid":26442,"tid":259,"ts":0},
{"args":{"name":"Chrome_IOThread"},"cat":"__metadata","name":"thread_name","ph":"M","pid":26404,"tid":35075,"ts":0},
{"args":{"name":"VizCompositorThread"},"cat":"__metadata","name":"thread_name","ph":"M","pid":26413,"tid":26115,"ts":0},
{"args":{"name":"CrGpuMain"},"cat":"__metadata","name":"thread_name","ph":"M","pid":26413,"tid":259,"ts":0},
{"args":{"name":"Chrome_ChildIOThread"},"cat":"__metadata","name":"thread_name","ph":"M","pid":26442,"tid":19715,"ts":0},
{"args":{"name":"ThreadPoolBackgroundWorker"},"cat":"__metadata","name":"thread_name","ph":"M","pid":26413,"tid":4
@alexbezhan
alexbezhan / profile-after.json
Last active February 10, 2023 11:24
Htmx 100x performance boost
This file has been truncated, but you can view the full file.
[{"args":{"name":"swapper"},"cat":"__metadata","name":"thread_name","ph":"M","pid":0,"tid":0,"ts":0},
{"args":{"name":"CrBrowserMain"},"cat":"__metadata","name":"thread_name","ph":"M","pid":67620,"tid":259,"ts":0},
{"args":{"name":"Compositor"},"cat":"__metadata","name":"thread_name","ph":"M","pid":97368,"tid":22787,"ts":0},
{"args":{"name":"ThreadPoolServiceThread"},"cat":"__metadata","name":"thread_name","ph":"M","pid":67629,"tid":24323,"ts":0},
{"args":{"name":"Chrome_IOThread"},"cat":"__metadata","name":"thread_name","ph":"M","pid":67620,"tid":28931,"ts":0},
{"args":{"name":"VizCompositorThread"},"cat":"__metadata","name":"thread_name","ph":"M","pid":67629,"tid":27907,"ts":0},
{"args":{"name":"CrRendererMain"},"cat":"__metadata","name":"thread_name","ph":"M","pid":97368,"tid":259,"ts":0},
{"args":{"name":"CrGpuMain"},"cat":"__metadata","name":"thread_name","ph":"M","pid":67629,"tid":259,"ts":0},
{"args":{"name":"ThreadPoolBackgroundWorker"},"cat":"__metadata","name":"thread_name","ph":"M","pid":67629,"tid
/**
* Lookup property in obj[path]
* @return found property in a nested object
*/
fun findNestedProperty(obj: Any, path: List<String>): NestedPropertyLookup {
var instance: Any = obj
for (keyIdx in path.indices) {
val memberProperties = instance::class.memberProperties
val property = memberProperties.find { callable ->
callable.name == path[keyIdx]
@alexbezhan
alexbezhan / gist:9bb140dc25c06cdfd56bc748c7fa9c19
Last active October 19, 2021 23:22
Scala Futures vs Kotlin Coroutines comparison

Scala with Futures:

import java.util.UUID
import scala.concurrent.Future

trait User {
    def isAdmin: Boolean
    def id: UUID
@alexbezhan
alexbezhan / Retrofit2Https.kt
Created June 19, 2018 08:21
How to call Https with Retrofit 2 and OkHttpClient
object Retrofit2Https {
fun createHttpClient(context: Context): OkHttpClient {
// Loads "my_certificate.crt" from res/raw/my_certificate.crt
val cf: CertificateFactory = CertificateFactory.getInstance("X.509")
val caInput: InputStream = BufferedInputStream(context.resources.openRawResource(R.raw.my_certificate))
val ca: X509Certificate = caInput.use {
cf.generateCertificate(it) as X509Certificate
}
// Create a KeyStore with the certificate