JavaScript – run once without booleans

An alternative way that overwrites a function when executed so it will be executed only once. function useThisFunctionOnce(){ // overwrite this function, so it will be executed only once useThisFunctionOnce = Function(“”); // real code below alert(“Hi!”); } // displays “Hi!” useThisFunctionOnce(); // does nothing useThisFunctionOnce(); ‘Useful’ example: var preferences = {}; function read_preferences(){ // … Read more

Why is a CPU branch instruction slow?

A branch instruction is not inherently slower than any other instruction. However, the reason you heard that branches should avoided is because modern CPUs follow a pipeline architecture. This means that there are multiple sequential instructions being executed simultaneously. But the pipeline can only be fully utilised if it’s able to read the next instruction … Read more

Shader optimization: Is a ternary operator equivalent to branching?

Actually this depends on the shader language one uses. In HLSL and Cg a ternary operator will never lead to branching. Instead both possible results are always evaluated and the not used one is being discarded. To quote the HLSL documentation: Unlike short-circuit evaluation of &&, ||, and ?: in C, HLSL expressions never short-circuit … Read more

Most Efficient Multipage RequireJS and Almond setup

I think you’ve answered your own question pretty clearly. For production, we do – as well as most companies I’ve worked with option 3. Here are advantages of solution 3, and why I think you should use it: It utilizes the most caching, all common functionality is loaded once. Taking the least traffic and generating … Read more

Can I use the “null pointer optimization” for my own non-pointer types?

As of Rust 1.28, you can use std::num::NonZeroU8 (and friends). This acts as a wrapper that tells the compiler the contents of a number will never contain a literal zero. It’s also why Option<Box<T>> is pointer-sized. Here’s an example showing how to create an Age and read its payload. use std::num::NonZeroU8; struct Age(NonZeroU8); impl Age … Read more

tech