Understanding V8 Hidden Classes, Java Logical & Bit Operations, and Go type func()
The article explains V8 hidden classes and inline caches for faster property access, details Java’s logical versus bitwise operators and short‑circuit evaluation, and demonstrates Go’s first‑class function types such as type func() for flexible, reusable code patterns.
This issue of the "Code Language Charm" technical station presents three parts:
1. A brief look at V8 hidden classes and inline caches.
2. An analysis of Java logical operations and bitwise operations.
3. Understanding Go's type func() and its practical uses.
Part 1: V8 Hidden Classes and Inline Caches
JavaScript is a dynamic, prototype‑based language, and V8 is a popular JavaScript engine. V8 introduces hidden classes to optimize property access. When a Person object is created, V8 generates hidden classes C0, C1, C2 as properties are added. Adding a new property (e.g., gender ) creates a new hidden class C3. Deleting a property destroys the hidden class chain, causing severe performance loss (e.g., 230 ms vs. 4 ms in a tight loop). The article recommends avoiding delete in performance‑critical code and using null assignment instead.
function Person(name, age) {
this.name = name;
this.age = age;
}
const zhangsan = new Person('Zhangsan', 20);
const xiaofang = new Person('Xiaofang', 21);
xiaofang.gender = 'female';V8 also employs inline caches: if the same property is accessed repeatedly with the same hidden class, the engine caches the lookup, dramatically speeding up execution. The article advises avoiding defining functions inside other functions to keep inline caches effective.
Part 2: Java Logical and Bitwise Operations
Java provides a rich set of operators: arithmetic, assignment, logical (&&, ||, !, |, &), relational, increment/decrement, and bitwise (&, |, ^, ~, <<, >>, >>>). The article explains the semantics of each logical operator, shows truth tables, and highlights the difference between short‑circuit operators (&&, ||) and their non‑short‑circuit counterparts (&, |).
Operator
Usage
Meaning
Description
Example
Result
&&
a && b
Short‑circuit AND
True only if both a and b are true; b is not evaluated if a is false.
3 > 2 && 3 < 4
true
||
a || b
Short‑circuit OR
True if either a or b is true; a is not evaluated if a is true.
3 < 2 || 3 > 4
false
!
!a
Logical NOT
Inverts a boolean value.
!(3 > 4)
true
Practical examples demonstrate short‑circuit evaluation to avoid costly operations:
public class Test {
public static void main(String[] args) {
// Scheme 1: a() && b()
System.out.println(a() && b());
// Scheme 2: b() && a()
System.out.println(b() && a());
}
public static boolean a() {
System.out.println("do a!");
boolean a = false;
for (int i = 0; i < 1000000; i++) {
a = true;
}
return a;
}
public static boolean b() {
System.out.println("do b!");
return false;
}
}The article also covers bitwise operators (&, |, ^, ~, <<, >>, >>>) and shows how they can be used to store multiple boolean flags efficiently, such as Android view state flags. A concrete example uses an int as a bitmask to represent seven colors, demonstrating setting, testing, and clearing bits.
public class Test {
private static int mColors = 0B00000000;
private static final int RED = 0B00000001;
private static final int ORANGE = 0B00000010;
private static final int YELLOW = 0B00000100;
private static final int GREEN = 0B00001000;
private static final int BLUE = 0B00010000;
private static final int PURPLE = 0B00100000;
private static final int PINK = 0B01000000;
public static void main(String[] args) {
// Set red, green, blue
mColors = mColors | RED | GREEN | BLUE;
System.out.println(mColors);
// Test red
System.out.println((mColors & RED) != 0);
// Test yellow (false)
System.out.println((mColors & YELLOW) != 0);
// Add yellow
mColors |= YELLOW;
System.out.println((mColors & YELLOW) != 0);
// Remove yellow
mColors &= ~YELLOW;
System.out.println((mColors & YELLOW) != 0);
}
}Part 3: Understanding Go's type func()
Go allows defining custom types with the type keyword, including function types. A function type specifies only the parameter and result types, not names. Functions can be assigned to variables of that type, passed as arguments, and returned from other functions, making them first‑class citizens.
type PowerCanculator func(num int) int // function type
func PowerBase2(num int) int { return 1 << num }
var pc PowerCanculator = PowerBase2
result := pc(2) // result == 4The article shows a generic calculator that receives a PowerCanculator to compute powers with different bases, and a generator function that returns a specific implementation based on a base value.
func PowerCanculatorGenerator(base int) PowerCanculator {
switch base {
case 2:
return PowerBase2
case 4:
return PowerBase4
default:
return nil
}
}
func main() {
pc := PowerCanculatorGenerator(2)
fmt.Println(pc(2)) // 4
pc = PowerCanculatorGenerator(4)
fmt.Println(pc(2)) // 16
}In real applications, function types are used for HTTP routing in Go's net/http package, where type HandlerFunc func(ResponseWriter, *Request) allows registering handlers like http.HandleFunc("/sayhi", hi) .
Overall, the article emphasizes two key properties of functions in Go: they can be passed as parameters and returned as values, enabling flexible and reusable code designs.
Conclusion: Paying attention to language‑specific features such as hidden classes, short‑circuit evaluation, bitwise flags, and first‑class functions can lead to more efficient and maintainable code.
Baidu Geek Talk
Follow us to discover more Baidu tech insights.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.